zoukankan      html  css  js  c++  java
  • GridView中CheckBox翻页记住选项

    <asp:GridView ID="gvYwAssign" runat="server" AutoGenerateColumns="False"
    AllowPaging="True" OnPageIndexChanging="gvYwAssign_PageIndexChanging"
    DataSourceID="ObjectDs" OnRowDataBound="gvYwAssign_RowDataBound" PageSize="5">
    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    <asp:CheckBox ID="ckbSelAll" runat="server" Text="全选" OnCheckedChanged="cbSelAll_CheckedChanged" AutoPostBack="true" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="ckbxSel" runat="server" ToolTip='<%#Eval("id") %>' OnCheckedChanged="ckbxSel_CheckedChanged" AutoPostBack="true" />
    </ItemTemplate>
    </asp:TemplateField>

    </Columns>
    </asp:GridView>

    --代码页

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Default2JinE : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    string[] strallItems = new string[] { };
    ViewState["allItems"] = strallItems;
    }
    }

    //单个选中与取消
    protected void ckbxSel_CheckedChanged(object sender, EventArgs e)
    {
    string[] strSelectFen = (string[])ViewState["allItems"];
    List<string> listSelect = strSelectFen.ToList();
    CheckBox chk = (CheckBox)sender;
    DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent; //这个对象的父类为cell
    GridViewRow gr = (GridViewRow)dcf.Parent; //cell的父类就是row,这样就得到了该checkbox所在的该行
    string nowId = chk.ToolTip;
    if (true == chk.Checked)
    {
    if (false == listSelect.Contains(nowId))
    {
    listSelect.Add(nowId);
    }
    }
    else
    {
    if (true == listSelect.Contains(nowId))
    {
    listSelect.Remove(nowId);
    }
    }
    strSelectFen = listSelect.ToArray();
    ViewState["allItems"] = strSelectFen;
    }

    //全选与取消
    protected void cbSelAll_CheckedChanged(object sender, EventArgs e)
    {
    string[] strSelectFen = (string[])ViewState["allItems"];
    List<string> listSelect = strSelectFen.ToList();
    string nowId;

    CheckBox cb1 = gvYwAssign.HeaderRow.FindControl("ckbSelAll") as CheckBox;
    if (cb1.Checked)//选中
    {
    for (int i = 0; i < gvYwAssign.Rows.Count; i++)
    {
    CheckBox cb2 = gvYwAssign.Rows[i].FindControl("ckbxSel") as CheckBox;
    cb2.Checked = true;

    nowId = cb2.ToolTip;
    if (false == listSelect.Contains(nowId))
    {
    listSelect.Add(nowId);
    }
    }
    }
    else//取消
    {
    for (int i = 0; i < gvYwAssign.Rows.Count; i++)
    {
    CheckBox cb2 = gvYwAssign.Rows[i].FindControl("ckbxSel") as CheckBox;
    cb2.Checked = false;

    nowId = cb2.ToolTip;
    listSelect.Remove(nowId);
    }
    }

    strSelectFen = listSelect.ToArray();
    ViewState["allItems"] = strSelectFen;
    }

    //包括绑定原来选项
    protected void gvYwAssign_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    //绑定原来选项
    string[] strSelectFen = (string[])ViewState["allItems"];
    CheckBox chk = e.Row.FindControl("ckbxSel") as CheckBox;
    string nowId = chk.ToolTip;
    for (int j = 0; j < strSelectFen.Length; j++)
    {
    if (nowId == strSelectFen[j])
    {
    chk.Checked = true;
    }
    }
    }

    }

    }

  • 相关阅读:
    jq获取滚动条高度
    用jq实现简单的tab选项卡
    bootstrap基础css样式1
    c3中的弹性盒子
    第一天注册
    Windows平台下结合 tortoiseSVN 和 VisualSVN Server 搭建SVN服务器并实现 web 站点同步
    【转载】IIS出现“HTTP 错误 500.0,C:phpphp-cgi.exe
    【转载】PHP5.3 配置文件php.ini-development和php.ini-production的区别
    解决 Composer-Setup.exe 安装过程中的报错
    IIS配置页面重写(配合插件url-rewrite2去除页面后缀名)
  • 原文地址:https://www.cnblogs.com/yubufan/p/7383777.html
Copyright © 2011-2022 走看看