获取gridview(Asp.net)中所选复选框的ID c#

时间:2011-03-31 12:49:37

标签: c# asp.net

我有两列用于id&其他的复选框。 我在gridview中选中了复选框。 我想在gridview中看到检查的值,如果选中复选框,那么我想要那些值,即id Asp.net

4 个答案:

答案 0 :(得分:4)

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

   int uPrimaryid= gvr.cells["uPrimaryID"];
 }
}

答案 1 :(得分:1)

您需要做的是使用模板字段:

<asp:TemplateField HeaderText="Field">
                            <ItemTemplate>
                                <div style="display: block">
                                    <asp:Checkbox Checked='<%# DataBinder.Eval(Container.DataItem,"Field") %>'
                                        runat="server" ID="chkField"></asp:Label>
                                </div>
                            </ItemTemplate>
                        </asp:TemplateField>

然后你可以这样做:

foreach (DataGridRow dr in DataGrid1.Rows)
{
    ((CheckBox)gvr.FindControl("chkField")).Checked
}

查看是否已选中

答案 2 :(得分:1)

在你的aspx中你有以下

<asp:GridView ID="gridViewID" runat="server" DataKeyNames="DataKey1,DataKey2,DataKey3" >
   <Columns>
      <asp:TemplateField HeaderText="selected">
         <ItemTemplate>
             <asp:CheckBox ID="checkBoxID" runat="server" 
                 Checked='<%# Bind("Selected") %>'
                 OnCheckedChanged="checkBoxID_CheckedChanged" 
                 AccessKey='<%# Container.DataItemIndex %>'  
                 AutoPostBack="True" />
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView>

然后在您的事件处理程序中执行与此类似的操作:

protected void checkBoxID_CheckedChanged(object sender, EventArgs e)
{
    var checkbox = (CheckBox)sender;
    var rowIndex = Convert.ToInt32(checkbox.AccessKey);
    var gridView = GetErhebungModulGridView();
    var dataKey = gridView.DataKeys[rowIndex];
    if (dataKey != null)
    {
        var dataKey1 = dataKey["DataKey1"];
        var dataKey2 = dataKey["DataKey2"];
        var dataKey3 = dataKey["DataKey3"];

        //Do something with the variables keys above

    }
}

答案 3 :(得分:0)

<asp:gridview id="gv" runat="server">
    <columns>
    <asP:TemplateField>
       <Asp:checkbox id="chk" runat="server" />
       <Asp:literal id="ltlID" runat="server" visible="false" text='<%#eval("ID")%>' />
    </asp:TemplateField>
    </columns>
</asp:gridview>

For each row as gridviewrow in gv.rows
    if ctype(row.findcontrol("chk"),checkbox).checked then
       Dim _ID as integer = ctype(row.findcontrol("ltlID"),literal).text
    end if
next