Saturday, December 5, 2009

A simple way to copy value of first control to the remaining controls in a ASP.NET Repeater.

A very simple example to copy value of first control to the remaining controls in Repeater on clicking the control which is placed in the Header Template of Repeater. You can download a complete example of it.

CS File:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CopyValues : System.Web.UI.Page
{
    // It will contain the concatenated string of all textbox controls' ClientIDs, which will be used at client side
    public string controlID = string.Empty;
    // It is the reference to the Image which we have placed in the header template of Repeater which will copy values of first Textbox to all remaining Textboxes
    Image imgCopyPaste = null;

    ///<summary>
    /// Bind Datasouce to the Repeater
    ///</summary>
    ///<param name="sender"></param>
    ///<param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        rptItems.DataSource = CreateDatasouce();
        rptItems.DataBind();
    }
    ///<summary>
    /// Datasource which we will bind to the Repeater Control
    ///</summary>
    ///<returns></returns>
    private ICollection CreateDatasouce()
    {
        // Create sample data for the Repeater control.
        DataTable dt = new DataTable();
        DataRow dr;

        // Define the columns of the table.        
        dt.Columns.Add(new DataColumn("ItemName", typeof(String)));
        dt.Columns.Add(new DataColumn("Qty", typeof(Int32)));

        // Populate the table with sample values.
        for (int i = 0; i < 9; i++)
        {
            dr = dt.NewRow();
            dr[0] = "item " + i.ToString();
            dr[1] = i;
            dt.Rows.Add(dr);
        }
        return dt.DefaultView;
    }
    protected void rptItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            // Get the Image which we have placed in the Header Template                      
            imgCopyPaste = e.Item.FindControl("imgCopyQty") as Image;
        }
        else if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            // Get each Textbox control's ClientID and build a concatenated string           
            controlID = controlID + e.Item.FindControl("txtCustomQty").ClientID + "|";
        }
        else if (e.Item.ItemType == ListItemType.Footer)
        {
            // Add the onclick event and pass the concatenated string of controls' ClientIDs           
            imgCopyPaste.Attributes.Add("onclick", "CopyQuantity('" + controlID.Substring(0, controlID.Length - 1) + "')");
        }
    }
}

ASPX File:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CopyValues.aspx.cs" Inherits="CopyValues" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <style type="text/css">       
    </style>
    <script type="text/javascript" language="javascript">
function CopyQuantity(controlID){

    var controlIdArray=controlID.split("|");
    if(document.getElementById(controlIdArray[0]).value=='')
        {
            alert('Please enter any quantity in the first item to paste in remaining items! ')
            return;
        }
    for(var i=1; i<controlIdArray.length; i++)
    {   
        document.getElementById(controlIdArray[i]).value=document.getElementById(controlIdArray[0]).value;
    }
}
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="rptItems" runat="server" OnItemDataBound="rptItems_ItemDataBound">
            <HeaderTemplate>
                <table style="border-right: lightseagreen 1px solid; border-top: lightseagreen 1px solid;
                    border-left: lightseagreen 1px solid; border-bottom: lightseagreen 1px solid">
                    <tr style="font-weight: bold; color: white; height: 21px; background-color: lightseagreen">
                       
                        <td>
                            <strong>Item Name </strong>
                        </td>
                        <td>
                            <strong>Quantity </strong>
                        </td>
                        <td>
                            <strong>Value</strong><asp:Image ID="imgCopyQty" runat="server" Visible="true" ImageUrl="~/Images/copy_paste.png"
                                ToolTip="Copy Quantity"></asp:Image></td>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                   
                    <td>
                        <%# Eval("ItemName") %>
                    </td>
                    <td>
                        <%# Eval("Qty") %>
                    </td>
                    <td>
                        <asp:TextBox ID="txtCustomQty" runat="server" Width="30px"></asp:TextBox></td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </form>
</body>

How to check all checkboxes in Repeater Control?

The following code will demonstrate how to check all checkboxes in a Repeater control. A very simple way by using codebehind and javascript. You can download a complete example of it.

CS File:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ToggleCheckBoxes : System.Web.UI.Page
{

    // Declare two global variables
    // It will contain the concatenated string of all checkbox controls' names, which will be used at client side
    string childControlArray = string.Empty;
    // It is the reference to the checkbox which we have placed in the header template of Repeater which will toggle all checkboxes
    CheckBox chkSelect = null;

    ///<summary>
    /// Bind Datasouce to the Repeater
    ///</summary>
    ///<param name="sender"></param>
    ///<param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        rptItems.DataSource = CreateDatasouce();
        rptItems.DataBind();
    }
    ///<summary>
    /// Datasource which we will bind to the Repeater Control
    ///</summary>
    ///<returns></returns>
    private ICollection CreateDatasouce()
    {
        // Create sample data for the Repeater control.
        DataTable dt = new DataTable();
        DataRow dr;

        // Define the columns of the table.        
        dt.Columns.Add(new DataColumn("ItemName", typeof(String)));
        dt.Columns.Add(new DataColumn("Qty", typeof(Int32)));

        // Populate the table with sample values.
        for (int i = 0; i < 9; i++)
        {
            dr = dt.NewRow();
            dr[0] = "item " + i.ToString();
            dr[1] = i;
            dt.Rows.Add(dr);
        }
        return dt.DefaultView;
    }
    protected void rptItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            // Get the checkbox which we have placed in the Header Template
            chkSelect = e.Item.FindControl("chkSelect") as CheckBox;

        }
        else if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            // Get each checkbox control's ClientID and build a concatenated string
            childControlArray = childControlArray + e.Item.FindControl("chkSelect").ClientID + "|";

        }
        else if (e.Item.ItemType == ListItemType.Footer)
        {
            // Add the onclick event and pass the concatenated string of controls' ClientIDs
            chkSelect.Attributes.Add("onclick", "ToggleAllCheckBoxes(this,'" + childControlArray + "')");
        }
    }

}

ASPX File:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ToggleCheckBoxes.aspx.cs"
    Inherits="ToggleCheckBoxes" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <style type="text/css">       
    </style>
    <script type="text/javascript" language="javascript">

function ToggleAllCheckBoxes(mainControl,childControlString)
{

    var childControlArray = childControlString.split("|");

        for(var i=0; i < childControlArray.length-1; i++)
        {
            document.getElementById(childControlArray[i]).checked=document.getElementById(mainControl.id).checked;
        }
}

</script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="rptItems" runat="server" OnItemDataBound="rptItems_ItemDataBound"
            >
            <HeaderTemplate>
                <table style="border-right: lightseagreen 1px solid; border-top: lightseagreen 1px solid;
        border-left: lightseagreen 1px solid; border-bottom: lightseagreen 1px solid">
                    <tr style="font-weight: bold; color: white; height: 21px; background-color: lightseagreen">
                        <td>
                            <asp:CheckBox runat="server" ID="chkSelect" Text="Select All" Font-Bold="true" />
                        </td>
                        <td>
                            <strong>Item Name </strong>
                        </td>
                        <td>
                            <strong>Quantity </strong>
                        </td>
                   
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:CheckBox ID="chkSelect" runat="server" />
                    </td>
                    <td>
                        <%# Eval("ItemName") %>
                    </td>
                    <td>
                        <%# Eval("Qty") %>
                    </td>
                   
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </form>
</body>

Thursday, October 22, 2009

How to find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million?

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even-valued terms in the sequence which do not exceed four million.

Note: Solve yourself before going directly to the solution

Solution:
Language: C#
int term1 = 1, term2 = 2;
int sum = 0;

while (term2 <= 4000000)
{
if ((term2 % 2) == 0)
      {
       sum += term2;
      }            
      // Switch values
      term1 = term1 + term2;
      term2 = term1 - term2;
      term1 = term1 - term2;
      // calculate next value by adding previous two terms
      term2 = term1 + term2;
}

How to add all the natural numbers below one thousand that are multiples of 3 or 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
How:

  1. First we determine how many multiples are of 3, 5 and 15
  2. Multiples of 3 are 333 => Take Quotient of (1000/3) which is 333
  3. Similarly Multiples of 5 below 1000 are 199 => Take Quotient of (1000/5) which is 199
  4. We need to sum up the multiples of 3 or 5 but there exist some numbers which are multiples of 3 as well as of 5 so we need to subtract that common multiples
  5. We determined that multiples of 15 are that numbers which are multiples of 3 as well as of 5 and multiples of 15 below 1000 are 66 => Take quotient of (1000/15) which is 66

public void SumUpMultiples()
{
    int sum = 0;
    // From 1 to 66, we sum up the multiples of 3,5 but subtract multiples of 15
     for (int i = 1; i <= 66; i++)
     {
         sum += ((i * 3) + (i * 5)) - (i * 15);
     }
    // From 67 to 199, we sum up the multiples of 3,5 only
     for (int j = 67; j <= 199; j++)
     {
         sum += (j * 3) + (j * 5);
     }
    // From 200 to 333, we sum up the multiples of 3 only
     for (int k = 200; k <= 333; k++)
     {
         sum += (k * 3);
     }
}

Sunday, October 11, 2009

My First Day At Axact

A short story about my first day at Axact :)
I was so excited that I’m going to join such a big company which claims that Axact Pakistan is the largest single-entity IT infrastructure of Pakistan and is one of the world’s top 10 fastest growing companies of its size but a little bit afraid about the new company, new place, new people, working environment and type of work etc.


I started my journey on 1st Oct, 2009 at 11:15 am from ALLAMA IQBAL INTERNATIONAL AIRPORT LAHORE. It was my first flight and first visit to Karachi where I required to spend 3 months at Axact Karachi office and then come back to Lahore office because my hiring was for Axact Lahore office.

My first traveling by air was good experience and I landed at JINNAH INTERNATIONAL AIRPORT KARACHI after traveling of about 75 minutes where special Axact Guest Relation Officer (GRO) was waiting for us with Axact Corporate Van to pick from airport and drop at Axact House Karachi.

First day was our orientation day; first of all we go to the Axact Training Room where some documentation process was done and then GRO took us to the Axact Cafeteria for refreshment. After that we came back to the Training Room where a video about the Axact’s Vision, Mission & Values was played and it took 2 hours at least. This video was the Virtual Tour to the Axact House Infrastructure, working environment and lifestyle.

We were eight people in total, six of Offshore Communication Department from Islamabad and two of Software Department from Lahore and me definitely one of them. After this session we took complimentary lunch and were transferred to the respective departments. In my department, my workstation was ready and given to me the credentials to access it.

After some time, a complete visit of the Axact House was arranged and GRO took us with him and we visited each and every department and observed the lifestyle which the Axact House claims. After that I came back to my workstation where the Axact branded mug given to me and the floor steward served me tea in the Axact branded cups.


In Axact, every new employee gets a welcome cake in the Axact branded packing with complimentary card to share with your family.

Inside the wrapped Gift Pack :)



A special compliment from Axact to you and your parents.

Welcome to Axact Greeting Card.



After that I went to the workstation of each member of my Department with Department Coordinator to introduce myslef and it is called Departmental Orientation. First day, I signed out of office at 12 am and went to the Axact Corporate Guest House to take rest.

That was my first day :)

  © Blogger template 'Minimalist F' by Ourblogtemplates.com 2008

Back to TOP