GridView CheckBoxes-在隐藏代码的调试过程中未选中的所有字段 [英] GridView CheckBoxes- All fields showing unchecked during debugging in code-behind
问题描述
我的GridView包含2列(UserId和一个CheckBox字段).
用户通过选中复选框来选择几行.
调试时,它显示为false(即未检查),因此它未执行"if(cb.Checked)"中的代码.
我找不到错误的出处.
//================================================ ==========
My GridView contains 2 column (UserId and a CheckBox field).
The user is selecting a few rows by checking in the checkBox.
While debugging, it shows false (ie.unchecked) and therefore it is not executing the code within "if (cb.Checked)".
I am not able to find where I did the mistake.
//==========================================================
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false">
<Columns >
<asp:TemplateField >
<ItemTemplate >
<asp:Label ID="Label1" runat="server" Text='<%#Eval("UserId")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate >
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
//================================================ ==========
//==========================================================
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("CheckBox1");
if (cb.Checked)
{
string aa = "wkwk";
}
}
}
推荐答案
我可以找到错误.
代码就是这样.
I could find the error.
The code was like this.
protected void Page_Load(object sender, EventArgs e)
{
//Databinding to GridView
}
//================================================ ===================
更正后的代码如下
//================================================ ==================
//===================================================================
The corrected code is as follows
//===================================================================
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Databinding to Gridview
}
}
这篇关于GridView CheckBoxes-在隐藏代码的调试过程中未选中的所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!