开发者

Get the id of selected checkboxes in gridview (Asp.net) c#

I have two columns one for id & other for checkboxes. i have taken checkboxes inside the gridview. i wanted to see the checked values inside the gridview , If checkboxes are 开发者_运维技巧checked then i want those values i.e id Asp.net


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

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


What you will have to do is use a template field:

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

Then you can do:

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

to see if it's checked


in your aspx you have the following

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

Then in your event handler you do something similar to this:

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

    }
}


<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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜