里面的GridView复选框选中状态 [英] CheckBox Checked state inside gridview

查看:144
本文介绍了里面的GridView复选框选中状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的GridView

I have a simple gridview

            <asp:GridView ID="GridView1" runat="server" DataKeyNames="OriginatorID" AutoGenerateColumns="False"
            AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5"
            OnPreRender="GridView1_PreRender">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Originator" HeaderText="Originator" />
            </Columns>
        </asp:GridView>

我打电话以下在 GridView1_PageIndexChanging 事件

        foreach (GridViewRow item in GridView1.Rows)
        {
            try
            {
                if (item.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = (CheckBox)(item.Cells[0].FindControl("CheckBox1"));

                    // chk.checked will access the checkbox state on button click event
                    if (chk.Checked)
                    {
                        //code if checked
                    }
                    else
                    {

                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

问题,如果我选择一个复选框,并选择gridview的下一个页面就不会执行里面

problem if I select a checkbox and select the next page on gridview it never execute the code inside

 if (chk.Checked)

尽管我已经检查了chekBoxes作为托运它没有得到他们的状态。

even though I have checked the chekBoxes it's not get their state as checked.

为什么会发生这种事?

推荐答案

试试这个:
结果

Try this:

检查Wheather你已经把你的code对结果数据绑定到GridView控件

Check Wheather you have put your code for binding data to GridView in

If (!IsPostBack)
{
     //Code for Binding Data to GridView 
} 

这篇关于里面的GridView复选框选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆