从复选框中获取GridView中的ID
本文关键字:中的 ID GridView 获取 复选框 | 更新日期: 2023-09-27 18:01:06
我有以下GridView-
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="checkButton_OnClick" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="tID" DataField="tID"
SortExpression="tID" HeaderStyle-CssClass = "hideGridColumn" ItemStyle-CssClass="hideGridColumn"></asp:BoundField>
<asp:BoundField HeaderText="Name" HeaderStyle-HorizontalAlign=Center DataField="NAME"
SortExpression="NAME" ItemStyle-HorizontalAlign=Center></asp:BoundField>
</Columns>
然后找到复选框由-检查的行
foreach (GridViewRow gvr in table_example.Rows)
{
if (((CheckBox)gvr.FindControl("CheckBox1")).Checked == true)
{
//Here I need the tID of the row that is checked
WebService1 ws = new WebService1();
ws.addID(tID);
}
}
我需要获取选中行的tID
,我尝试了-
int tID = gvr.cells["tID"];
然而,这并没有奏效。
要获取当前行ID,您需要执行以下操作:gvr。身份证件但我不确定您是否真的想使用行ID或索引。我想你正在寻找这样的东西(考虑到索引1指的是"tID"BoundField(:gvr。单元格[1].Text.
foreach (GridViewRow gvr in table_example.Rows)
{
if (((CheckBox)gvr.FindControl("CheckBox1")).Checked == true)
{
//Here I need the tID of the row that is checked
int tID = Convert.ToInt32(gvr.Cells[1].Text);
WebService1 ws = new WebService1();
ws.addID(tID);
}
}
试试这个。
没有必要在checkButton_OnClick事件上进行此循环,您可以按如下方式编写代码:
protected void checkButton_OnClick (object sender, EventArgs e)
{
CheckBox chk= (CheckBox)sender;
GridViewRow gridViewRow = (GridViewRow)chk.NamingContainer;
int rowindex = gridViewRow.RowIndex;
}