<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;
}
}
}

}

}

GridView中CheckBox翻页记住选项的更多相关文章

  1. Dynamics CRM2013 sub grid中数据翻页问题

    CRM2013中一个很低级很令人无语的BUG,见下图subgrid中的明细条目超过当前页设置的条目后会有翻页,在底下有个paging bar会显示条数.页数.当前所处页数 但sp1版本的CRM打上ur ...

  2. asp.net,根据gridview 中checkbox复选框选中的行对数据库进行操作

    在asp.net中,使用checkbox,对gridview添加复选框. 多选数据行后,根据已选数据,对原数据进行多条语句查询. string sql = "Select * from 表 ...

  3. GridView中CheckBox单击事件(oncheckedchanged)

    在GridView中加入 CheckBox控件,想通过单击选中出现如下图所示效果: 具体做法是: 前台GV部份省掉.只加关键的CheckBox部份. view plaincopy to clipboa ...

  4. GridView中CheckBox的用法

    以下是我在GridView中控制CheckBox的全选/取消全选,根据子CheckBox处理全选CheckBox状态的操作并获取所选GridView中所选行的列的值. 脚本代码:    <scr ...

  5. Linux的vim编辑器中的翻页命令

    当我们进入Linux的vim编辑器查看脚本时,按上下键查看是不是非常慢?这个时候就要用到我们的翻页快捷键了,快捷键命令如: 整页翻页命令为:Ctrl + f 键   f 的英文全拼为:forward: ...

  6. centos中less翻页查询的用法

    用法实例: cat 21342.log | less

  7. 模板列传值到子窗体中,子窗体中多选gridview中checkbox保存数据多项到数据库中

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  8. js gridview中checkbox的全选与全不选

    1.html: <asp:GridView runat="server" ID="gvAddBySR" AutoGenerateColumns=" ...

  9. bootStrap中的翻页效果

    <div class="container"> <br/> <ul class="pagination"> <li&g ...

随机推荐

  1. 2018SDIBT_国庆个人第三场

    A - A CodeForces - 1042A There are nn benches in the Berland Central park. It is known that aiai peo ...

  2. Servlet基本_オブジェクトのスコープ

    1.スコープ種類Servletには以下のスコープがあります.Request.Session.Applicationの順にスコープは広くなっていきます.・Applicationスコープ:アプリケーション ...

  3. Android 操作UI线程的一些方法

    我们经常会在后台线程中去做一些耗时的操作,比如去网络取数据.但是当数据取回来,需要显示到页面上的时候,会遇到一些小麻烦,因为我们都知道,android的UI页面是不允许在其他线程直接操作的.下面总结4 ...

  4. C++ MFC常用函数(转)

    WinExec() ExitWindowsEx() GlobalMemoryStatus() GetSystemInfo() GetSystemDirectory() GetWindowsDirect ...

  5. 解决:python 连接Oracle 11g 报错:ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务

    其次,将查询到的service_name替换sid即可:conn=cx_Oracle.connect('hr/admin@localhost:1521/EE.oracle.docker')

  6. CSS 字体风格

    粗体 font-weight 属性可以设置文本的粗细. 它有两个属性: normal 普通粗细 bold 粗文本 示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  7. Html----编写

    文档的编辑 选择文档---->修改文档后缀名(.thml)--->用浏览器打开(解释和执行HTML源代码的工具) 浏览器主要分类及内核 火狐(Firefor)--->Gecho IE ...

  8. Javascript概念

    什么是JavaScript? 1 以前的目的:验证表单输入的正确性. 2 现在的目的:多做一些页面的交互效果. 3 javascript是一个跨平台的脚本语言. 4 网景公司开发的,由布兰登·艾奇最先 ...

  9. 关于log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).的问题

    解决办法(非长久之计,折中) 将该方法插入到main函数中,可以自行打印日志信息了 BasicConfigurator.configure(); //自动快速地使用缺省Log4j环境.原文链接:htt ...

  10. jquery 页面传值 汉字

    function getURLParameter(name) { return decodeURIComponent( (new RegExp('[?|&]' + name + '=' + ' ...