[英]CheckBox “Checked” value in GridView is always false
I'm using a checkbox
column in a gridview
which is populated from a SQL
database. 我在
gridview
使用了一个checkbox
列,该列从SQL
数据库中填充。 A button below the gridview
should retrieve the data of the rows whose checkboxe's
have been checked. gridview
下面的按钮应该检索已经选中了checkboxe's
行的数据。 When I loop over all the rows checkboxe's
, none of them has checked==true
even though I have checked them all before clicking the button. 当我循环遍历所有行
checkboxe's
,即使我在单击按钮之前检查了所有行,也没有checked==true
。 Here is the ASP.NET
code: 这是
ASP.NET
代码:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
and here is the button's action that loops over all the rows' checkboxes: 这是按钮的动作循环遍历所有行的复选框:
protected void GetSelectedRecords(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
if (chkRow != null && chkRow.Checked) //chkRow.Checked is always "false"
{
string name = row.Cells[2].Text;
}
}
}
}
Thanks a lot in advance. 非常感谢提前。 I'd greatly appreciate any help.
我非常感谢任何帮助。
If you are binding your grid in Page_Load
, Make sure you are not binding your grid outside of if(!IsPostBack){}
. 如果要在
Page_Load
中绑定网格,请确保在if(!IsPostBack){}
之外没有绑定网格。 Otherwise you will loose the checkboxe's
on each postback
and therefore losing the status of checkboxe's
. 否则你将松开每个
postback
的checkboxe's
,因此失去了checkboxe's
状态。
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Bind Your Grid Here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.