请注意,DetailsView 使用其上的单元格而不是其拥有的每一行的控件 ID,因此您可以使用行、单元格和控件位置获取 CheckBoxField 值,如下所示:
// Page_Load is just an example event here, change to any event you need
protected void Page_Load(object sender, EventArgs e)
{
DetailsView dv = sender as DetailsView;
// the checkbox uses checked state as its value to be passed
// n = row/cell/control indexes where CheckBoxField has bound into, starting from 0
// e.g. dv.Rows[0].Cells[0].Controls[0] as CheckBox
bool checkboxvalue = (dv.Rows[n].Cells[n].Controls[n] as CheckBox).Checked;
}
如果您仍想使用FindControl,请使用ItemTemplate 包装元素并在其上创建CheckBox 控件(请注意,您可能需要在TemplateField 之上使用BoundField 和DataField="ThingEnabled"数据绑定):
<asp:DetailsView runat="server" ...>
<Fields>
...
<asp:TemplateField HeaderText="Thing Enabled">
<ItemTemplate>
<asp:CheckBox ID="ThingEnabled" runat="server" Checked="<%# Bind("ThingEnabled") %>">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
然后您可以使用FindControl 访问该复选框控件:
DetailsView dv = sender as DetailsView;
// n = row/cell indexes, starting from 0
bool checkboxvalue = (dv.Rows[n].Cells[n].FindControl("ThingEnabled") as CheckBox).Checked;
参考/类似问题:
How to get value from DetailsView Control in ASP.NET?
Get the value of a BoundField from a DetailsView
Accessing DetailsView check box value