checkbox.checked在网格视图中始终为false

时间:2014-06-23 14:09:51

标签: c# asp.net .net gridview

即使在gridview上检查,我总是将复选框检查属性设置为false。我已尝试在此论坛上的其他答案,例如enableviewstate设置为false但没有工作。 请帮忙。

这是我的设计

<form id="form1" runat="server">
<table class="style1">
    <tr>
        <td align="center">
            TPA Batch Auditing</td>
    </tr>
    <tr>
        <td>               
        </td>
    </tr>
    <tr>
        <td align="center">               
            <asp:GridView ID="GridView_BatchAuditing" runat="server" Width="80%" 
                EnableModelValidation="True" >
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" EnableViewState="true" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <HeaderStyle BackColor="#3399FF" />
            </asp:GridView>               

            <div id="DivNoRec" runat="server">
            <table class="style1" border="1">
                <tr>
                    <td bgcolor="#3399FF">
                        &nbsp;</td>
                    <td bgcolor="#3399FF">
                        &nbsp;</td>
                    <td bgcolor="#3399FF">
                        &nbsp;</td>
                    <td bgcolor="#3399FF">
                        &nbsp;</td>
                </tr>
                <tr>
                    <td width="25%">
                        &nbsp;</td>
                    <td align="center" colspan="2">
                        <strong>No Record Found</strong></td>
                    <td width="25%">
                        &nbsp;</td>
                </tr>
            </table>
            </div>
        </td>
    </tr>
    <tr>
        <td align="center">
            <asp:Button ID="Button_BatchAuditing" runat="server" Font-Bold="True" 
                Height="44px" onclick="Button_BatchAuditing_Click" Text="BatchAuditing" 
                Width="142px" Font-Size="Small" />
        </td>
    </tr>
</table>
</form>

C#代码

protected void Button_BatchAuditing_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvRow in GridView_BatchAuditing.Rows)
    {
        CheckBox cbx = (CheckBox)gvRow.FindControl("CheckBox1");
        if (cbx.Checked == true)
        {     
            //int i=0;
            //my code
            //cbx.Checked = false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

确保您在Page_Load事件上绑定数据时使用了isPostback属性。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //code to bind data to gridview
    }

}