Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have a data gird in my page that my gird have checkbox and i have to loop on checkbox and if checked add too collection but i dont undrestand How can??
this is my grid
ASP
<asp:DataGrid ID="grdData" runat="server" AllowPaging="True" AutoGenerateColumns="False"
  HorizontalAlign="Center" Width="100%" DataKeyField="ContactID" CellPadding="4"
  ForeColor="#333333" GridLines="None" OnDeleteCommand="grdData_DeleteCommand">
  <itemstyle backcolor="#E3EAEB" />
  <pagerstyle horizontalalign="Center" backcolor="#666666" forecolor="White" />
  <alternatingitemstyle backcolor="White" />
  <columns>
   <asp:TemplateColumn>
    <itemtemplate>
     <input id="chkIsCheked" type="checkbox" title="<%#DataBinder.Eval(Container.DataItem, "ContactID")%>" />
    </itemtemplate>
   
   <asp:TemplateColumn HeaderText="کد مشتری" SortExpression="">
    <itemtemplate>
     <center>
      <%#DataBinder.Eval(Container.DataItem, "ContactID")%>
     </center>
    </itemtemplate>
   
   <asp:TemplateColumn HeaderText="نام  " SortExpression="">
    <itemtemplate>
     <center>
      <%#DataBinder.Eval(Container.DataItem, "FirstName")%>
     </center>
    </itemtemplate>
   
   <asp:TemplateColumn HeaderText="نام خانوادگی" SortExpression="">
    <itemtemplate>
     <center>
      <%#DataBinder.Eval(Container.DataItem, "LastName")%>
     </center>
    </itemtemplate>
   
   <asp:TemplateColumn HeaderText="ایمیل " SortExpression="">
    <itemtemplate>
     <center>
      <%#DataBinder.Eval(Container.DataItem, "PrimeryEmail")%>
     </center>
    </itemtemplate>
   
   <asp:TemplateColumn HeaderText="موبایل مشتری " SortExpression="">
    <itemtemplate>
     <center>
      <%#DataBinder.Eval(Container.DataItem, "PrimeryMobileNumber")%>
     </center>
    </itemtemplate>
   
   <asp:TemplateColumn>
    <itemtemplate>
     <a href="EmailSender.aspx?Email=<%#DataBinder.Eval(Container.DataItem, " primeryemail=")%>">
      فرستادن ایمیل با این شخص </a>
    </itemtemplate>
   
   <asp:TemplateColumn>
    <itemtemplate>
     <a href="ContactsDetaile.aspx?ID=<%#DataBinder.Eval(Container.DataItem, " contactid=")%>">
      دیدن جزئیات </a>
    </itemtemplate>
   
   <asp:ButtonColumn ButtonType="PushButton" HeaderText="حذف" CommandName="Delete">
    <itemstyle backcolor="Red" />
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <itemstyle horizontalalign="Center"></itemstyle>
   
  </columns>
  <edititemstyle backcolor="#7C6F57" />
  <footerstyle backcolor="#1C5E55" font-bold="True" forecolor="White" />
  <HeaderStyle BackColor="#1C5E55" ForeColor="White" Height="30px" HorizontalAlign="Center"
   VerticalAlign="Middle" Font-Bold="True" />
  <SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
Posted
Updated 22-Jan-11 10:25am
v2
Comments
jerrykid 23-Jan-11 1:10am    
Not clear, please provide more information about your question

1 solution

Hi,

This is the general solution, but I don't know what you want to achieve, so modify this according to your needs

private ArrayList GetCheckedItems(string ctrl, DataGrid grid)
{
    ArrayList checkedItems = new ArrayList();
    CheckBox chk;
    string strID = string.Empty;
	
    // Loop through each row in the GridView
    foreach(DataGridItem item in grid.Items)
    {
        chk = (CheckBox)item.Cells[0].Controls[1];

        strID = grid.DataKeys[item.ItemIndex].ToString();

        // Now see if the current CheckBox is checked
        if (chk.Checked)
        {
            // See if the current value is in the list, if not add it
            if (!(checkedItems.Contains(strID)))
            {
                checkedItems.Add(strID);
            }
        }
    }
	
	return checkedItems;
}


BTW. Try to google for this, you have many examples on net about this topic.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900