如果在gridview
上选中了复选框,我想获取隐藏字段的值我的gridview:
<asp:GridView ID="gv_enfant" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1"
Width="533px">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxenfant" runat="server" />
<asp:HiddenField ID="codeenfant" runat="server" Value='<%# Eval("codeEnfants") %>' />
</ItemTemplate>
..............
</asp:GridView>
以下是我如何遍历行并检查:
string myid = string.Empty;
for (int i = 0; i < gv_enfant.Rows.Count; i++)
{
CheckBox chbox = (CheckBox)gv_enfant.Rows[i].Cells[0].FindControl("CheckBoxenfant");
if (chbox.Checked)
{
myid = ((HiddenField)gv_enfant.Rows[i].Cells[0].FindControl("codeenfant")).Value;
}
}
我在条件上设置了断点,调试器从未命中该行
答案 0 :(得分:0)
并非所有行都有控件(例如页眉和页脚行)。我会这样试试:
string myid = string.Empty;
for (int i = 0; i < gv_enfant.Rows.Count; i++)
{
var chbox = gv_enfant.Rows[i].Cells[0].FindControl("CheckBoxenfant") as CheckBox;
var codeenfant = gv_enfant.Rows[i].Cells[0].FindControl("codeenfant") as HiddenField
if(chbox != null && codeenfant != null)
{
if (chbox.Checked)
{
myid = codeenfant.Value;
}
}
}
答案 1 :(得分:0)
您可以这样做:
string myid = string.Empty;
foreach (GridViewRow row in gvShows.Rows)
{
CheckBox chk = row.Cells[0].FindControl("CheckBoxenfant") as CheckBox;
if (chk != null && chk.Checked)
{
myid = ((HiddenField)row.Cells[0].FindControl("codeenfant")).Value;
}
}
此致