循环通过GridView复选框

时间:2012-03-09 22:05:49

标签: asp.net gridview

我试图遍历所有复选框并获取它们的值以及是否检查它们,并根据将包含记录ID的值将已检查/未检查的数据保存到数据库中。我试图通过asp.net做任何建议吗?

以下是代码:

            <Columns>
                <asp:TemplateField HeaderText="Application" ItemStyle-Width="25%">
                    <ItemTemplate>                        
                        <%#Eval("App")%>
                    </ItemTemplate>                    
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Edit" ItemStyle-Width="25%">
                    <ItemTemplate>
                        <input name="chkEdit" runat="server" value='<%# Eval("AppID") %>' class="checkbox" type="checkbox" checked='<%# Eval("Edit") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete" ItemStyle-Width="25%">
                    <ItemTemplate>                        
                        <input name="chkDelete" runat="server" value='<%# Eval("AppID") %>' class="checkbox" type="checkbox" checked='<%# Eval("Delete") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="View" ItemStyle-Width="25%">
                    <ItemTemplate>                        
                        <input name="chkView" runat="server" value='<%# Eval("AppID") %>' class="checkbox" type="checkbox" checked='<%# Eval("View") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>  

2 个答案:

答案 0 :(得分:2)

在onclick事件中

执行类似的操作。

foreach(Gridviewrow gvr in Gridview1.Rows)
 {
     if(((CheckBox)gvr.findcontrol("CheckBox1")).Checked == true)
     {

        //do something
     }
 }

答案 1 :(得分:2)

string[] controls = new string[] {"chkEdit","chkDelete","chkView"};

foreach (GridViewRow r in YourGridView.Rows)
{
   foreach(string cnt in controls){
     int value =  Convert.ToInt32(((System.Web.UI.HtmlControls.HtmlInputCheckBox)r.FindControl(cnt)).Value);
     bool isChecked = ((System.Web.UI.HtmlControls.HtmlInputCheckBox)r.FindControl(cnt)).Checked;
     //now you have id and checked/unchecked. use your query to save it to database
   }
}