GridView 翻页后保存CheckBox列的选中状态。

解决方案 »

  1.   

    把id拿出来存cookise里,翻页的时候判断记录,然后勾上
      

  2.   

    这就是页面间全传值了,简单的你直接用querystring吧,然后取到值后就直接保存状态了。
      

  3.   

    这个我以前研究过,这个用cookise比较浪费资源,你可以用字符串数组(全局变量) 储存   每次翻页的时候都进行一下对应的判断!
      

  4.   

    记住ID  在用JS 实现
      

  5.   


    <script language="javascript" type="text/javascript">
        //先获取所有的Checkbox
        var chkList = document.getElementsByName("CheckBox1");
    window.onload = function()
    {
    //为所有checkbox添加onclick事件处理,以自动更新“已选择的项”
    for(var i=0; i<chkList.length; i++)
    {
    chkList[i].onclick = chkClick;
    }
    }
    //checkbox的onclick事件,用于更新“已选择的项”
    function chkClick(){
    var checkedList = "";
    //获取所有被选中的项
    for(var i=0; i<chkList.length; i++){
    if(chkList[i].checked)
    checkedList += chkList[i].value + ",";
    }
    //把选中项的列表显示到“已选择的项”中,substring在这里是为了去除最后一个逗号
    document.getElementById("HiddenField1").value = checkedList.substring(0,checkedList.length-1);
    }
        function checkAll()
        {
            var chkall=document.getElementById("CheckBoxAll");
            if(chkall.checked)
            {
                var checkedList = "";
                for(var i=0;i<chkList.length;i++)
                {
                      chkList[i].checked=true;
                      checkedList += chkList[i].value + ",";
                }
                document.getElementById("HiddenField1").value = checkedList.substring(0,checkedList.length-1);
            }
            else
            {
                for(var i=0;i<chkList.length;i++)
                      chkList[i].checked=false;
                document.getElementById("HiddenField1").value="";
            }
        }
        </script>
    用自带分页 ,外部加全选的checkbox<asp:HiddenField ID="HiddenField1" runat="server" />
        <div id="container">
            <asp:GridView ID="GridView1" BorderColor="Black" OnRowDataBound="GridView1_RowDataBound"  AllowPaging="True" runat="server" AutoGenerateColumns="False"  Font-Size="12px" Width="549px" OnPageIndexChanging="GridView1_PageIndexChanging">
              <Columns>
                  <asp:TemplateField HeaderText="编号">
                      <ItemTemplate>
                          <input name="CheckBox1" type="checkbox" value="<%#Eval("ID") %>"/>
                      </ItemTemplate>
                  </asp:TemplateField>
                <asp:BoundField DataField="EmpID" HeaderText="账号" />
                <asp:BoundField DataField="EmpRealName" HeaderText="姓名" />
                <asp:BoundField DataField="EmpSex" HeaderText="性别" />
                <asp:BoundField DataField="EmpAddress" HeaderText="住址" />
                <asp:BoundField DataField="EmpZipCode" HeaderText="邮编" />
                <asp:BoundField DataField="EmpBirthday" HeaderText="生日" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False" />
                <asp:BoundField DataField="EmpSalary" HeaderText="薪水" DataFormatString="{0:c}" HtmlEncode="False" />
              </Columns>
              <HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
                <RowStyle HorizontalAlign="Center" />
                <PagerStyle HorizontalAlign="Center" />
            </asp:GridView>
            <br />
            全选:<input id="CheckBoxAll"  type="checkbox" onclick="checkAll()"  />
            &nbsp; &nbsp; &nbsp;
            <asp:Button ID="Button1" runat="server" Height="20px" Text="删 除" OnClick="Button1_Click" Width="59px" />
        
        </div>
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    /// <summary>
    /// Author:匆匆  Blog:http://www.cnblogs.com/huangjianhuakarl/
    /// </summary>
    public partial class 结合控件_GridviewJsCheckBox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void bind()
        {
            string sqlStr = "select * from Employee";
            DataSet myds = Common.dataSet(sqlStr);
            GridView1.DataSource = myds;
            GridView1.DataKeyNames = new string[] { "ID" };
            GridView1.DataBind();
        }
        /// <summary>
        /// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {        //遍历所有行设置边框样式
            foreach (TableCell tc in e.Row.Cells)
            {
                tc.Attributes["style"] = "border-color:Black";
            }
            
        }
        /// <summary>
        /// 在单击页导航按钮时发生,但在 GridView 控件执行分页操作之前。此事件通常用于取消分页操作。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            bind();
        }
        /// <summary>
        /// 删除所选记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            
            string type = HiddenField1.Value;
            string [] keyValue=type.Split(',');
            foreach(string  keyName in keyValue)
            {
                Common.ExecuteSql("delete from Employee where ID=" + keyName + "");
            }
            HiddenField1.Value = "";
            bind();
        }
    }
    仅供参考! 自己内容要修改!带全选,用的Gridview自带分页
      

  6.   

    在页面放一个input隐藏域来记录选定值,翻页的时候,把隐藏域的值也传递到下一页中,然后进行判断是否需要选中。