在DataGrid中循环以查找控件 [英] Loop In DataGrid for find control

查看:117
本文介绍了在DataGrid中循环以查找控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我的页面上有一个数据网格,我的网格有复选框,我必须在复选框上循环,如果选中则添加太多集合,但我不理解该怎么做? 这是我的网格

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: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" />

推荐答案



这是一般的解决方案,但我不知道您要实现什么,因此请根据您的需要进行修改

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;
}



顺便提一句.尝试在Google上搜索此内容,您会在网上找到许多与此主题相关的示例.



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


这篇关于在DataGrid中循环以查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆