带有选择按钮、边界字段和复选框的 Gridview。将数据绑定(bind)到 gridview 效果很好。 (数据库中的数据有一个用于 bounfield 的 NVARCHAR 列和一个用于复选框的 BIT 列。
通过“选择”按钮选择一行时,会触发代码隐藏中的事件,并将 GridView 中 2 个单元格的数据复制到页面上的 2 个控件:文本框和复选框。
第一个工作正常,我不知道如何检查 GridView 中的复选框是否被选中。我需要知道这一点,以便我可以相应地填充其他复选框控件。
(在粘贴代码之前:我刚刚花了大约 12 个小时在 SO 和其他地方寻找解决方案。众多条目都没有帮助。所以请耐心等待...)
<asp:GridView ID="grv_Test1" runat="server" CssClass="myGrid"
AutoGenerateColumns="False" DataKeyNames="Test1_First_Name"
OnRowCommand="grv_Test1_RowCommand">
<Columns>
<asp:CommandField SelectText="sel'" ShowSelectButton="True" ControlStyle-CssClass="btn btn-primary myBtn-xs">
</asp:CommandField>
<asp:BoundField DataField="Test1_First_Name" HeaderText="Name"><HeaderStyle Width="85px" />
</asp:BoundField>
<asp:CheckBoxField DataField="Test1_Active" HeaderText="Active">
</asp:CheckBoxField>
</Columns>
<HeaderStyle CssClass="myGridHeader" />
</asp:GridView>
后台代码:
int my_Data_From_Grid = Convert.ToInt32(e.CommandArgument);
txb_Test1_Name.Text = grv_Test1.Rows[my_Data_From_Grid].Cells[1].Text; // this works
cbx_Test1_Active.Text = grv_Test1.Rows[my_Data_From_Grid].Cells[2].Text; // NOT working
if (Convert.ToString(grv_Test1.Rows[my_Data_From_Grid].Cells[2].Text) == "1") // NOT working either
{ cbx_Test1_Active.Checked = true; }
else
{ cbx_Test1_Active.Checked = false; }
if (Convert.ToString(grv_Test1.Rows[my_Data_From_Grid].Cells[2].Text) == "True") // NOT working either
{ cbx_Test1_Active.Checked = true; }
else
{ cbx_Test1_Active.Checked = false; }
这是我选择迈克尔的行时得到的结果:
在 GridView 中,迈克尔处于“事件”状态,我需要“选中”顶部的复选框。
如何做呢...?非常感谢。
最佳答案
对于 CheckBoxFields 和 CheckBoxes,您需要获取 Checked
值才能知道它是否真正被选中。 Text
值实际上是 CheckBox 的另一个属性(请参阅 MSDN )。有时您会在复选框本身的左侧或右侧看到此文本。
所以你需要做的是首先获取CheckBox。然后使用该复选框的 Checked
属性。
CheckBox checkBox = (CheckBox)grv_Test1.Rows[my_Data_From_Grid].Cells[2].Controls[0];
cbx_Test1_Active.Checked = checkBox.Checked;
关于asp.net 检查gridview中的复选框是否被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35703369/