在GridView分页过程中,CheckBox的选中状态不丢失
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function SelectAll(tempControl)
{
//将除头模板中的其它所有的CheckBox取反
var theBox=tempControl;
xState=theBox.checked;
elem=theBox.form.elements;
for(i=0;i<elem.length;i++)
if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
{
if(elem[i].checked!=xState)
elem[i].click();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" AllowPaging="true" ID="gdvView"
AutoGenerateColumns="false" onpageindexchanging="gdvView_PageIndexChanging"
onrowcreated="gdvView_RowCreated"
onrowcommand="gdvView_RowCommand"
>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label runat="server" ID="labID" Text='<%# Eval("LinkmanId")%>'>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="客户名称">
<ItemTemplate>
<asp:Label runat="server" ID="labName" Text='<%# Eval("name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="联系人">
<ItemTemplate>
<asp:Label runat="server" ID="labPost" Text='<%# Eval("post") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="性别">
<ItemTemplate>
<asp:Label runat="server" ID="labGender" Text='<%# Eval("gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除" >
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="Delete" ID="lbDelete" Text="删除" OnClientClick='return confirm("确实要删除数据吗?")'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="全选">
<HeaderTemplate>
<asp:CheckBox runat="server" ID="checkAll" Text="全选" οnclick="javascript:SelectAll(this);"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="ckbSelect" OnCheckedChanged="ckbSelect_OnCheckedChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
<asp:Button runat="server" ID="btnSave" Text="保存" οnclick="btnSave_Click" />
</form>
</body>
</html>
<2>后台化码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
private readonly string ConnString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Check();
gdvView.DataSource = BindData();
gdvView.DataBind();
}
}
private DataTable BindData()
{
try
{
using (SqlConnection conn = new SqlConnection(ConnString))
{
conn.Open();
string strSql = "select LinkmanId,[name],post,gender from Linkman";
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = strSql;
comm.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dt=new DataTable();
da.Fill(dt);
return dt;
}
}
catch (Exception ex)
{
throw new Exception("在应用程序的请求过程中发生了如下错误:"+ex.ToString());
}
}
protected void gdvView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Check();
gdvView.PageIndex = e.NewPageIndex;
gdvView.DataSource = BindData();
gdvView.DataBind();
if (Session["UserList"] != null)
{
Hashtable ht = (Hashtable)Session["UserList"];
for (int i = 0; i < gdvView.Rows.Count; i++)
{
if(ht.ContainsKey(((Label)gdvView.Rows[i].Cells[0].FindControl("labID")).Text))
{
((CheckBox)gdvView.Rows[i].Cells[5].FindControl("ckbSelect")).Checked = true;
}
}
}
}
protected void gdvView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((Label)e.Row.FindControl("labID")).Visible = false;
}
}
private void Check()
{
Hashtable ht = new Hashtable();
if (Session["UserList"] != null)
{
ht = (Hashtable)Session["UserList"];
if (ht != null)
{
for (int i = 0; i < gdvView.Rows.Count; i++)
{
if (((CheckBox)gdvView.Rows[i].Cells[5].FindControl("ckbSelect")).Checked == true)
{
if (!ht.ContainsKey(((Label)gdvView.Rows[i].Cells[0].FindControl("labID")).Text))
{
ht.Add(((Label)gdvView.Rows[i].Cells[0].FindControl("labID")).Text, ((Label)gdvView.Rows[i].Cells[1].FindControl("labName")).Text);
}
}
else
{
ht.Remove(((Label)gdvView.Rows[i].Cells[0].FindControl("labID")).Text);
}
Session["UserList"] = ht;
}
}
else
{
for (int i = 0; i < gdvView.Rows.Count; i++)
{
if (((CheckBox)gdvView.Rows[i].Cells[5].FindControl("ckbSelect")).Checked == true)
{
ht.Add(((Label)gdvView.Rows[i].Cells[0].FindControl("labID")).Text, ((Label)gdvView.Rows[i].Cells[1].FindControl("labName")).Text);
}
}
}
}
else
{
for (int i = 0; i < gdvView.Rows.Count; i++)
{
if (((CheckBox)gdvView.Rows[i].Cells[5].FindControl("ckbSelect")).Checked == true)
{
ht.Add(((Label)gdvView.Rows[i].Cells[0].FindControl("labID")).Text, ((Label)gdvView.Rows[i].Cells[1].FindControl("labName")).Text);
}
Session["UserList"] = ht;
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
Check();
Hashtable ht = (Hashtable)Session["UserList"];
foreach (DictionaryEntry objDE in ht)
{
Response.Write(objDE.Value.ToString());
}
}
protected void ckbSelect_OnCheckedChanged(object sender, EventArgs e)
{
Check();
gdvView.DataSource = BindData();
gdvView.DataBind();
if (Session["UserList"] != null)
{
Hashtable ht = (Hashtable)Session["UserList"];
for (int i = 0; i < gdvView.Rows.Count; i++)
{
if (ht.ContainsKey(((Label)gdvView.Rows[i].Cells[0].FindControl("labID")).Text))
{
((CheckBox)gdvView.Rows[i].Cells[5].FindControl("ckbSelect")).Checked = true;
}
}
}
}
转载于:https://www.cnblogs.com/keerwzw/archive/2009/08/15/1546634.html
在GridView分页过程中,CheckBox的选中状态不丢失相关推荐
- html5中checkbox的选中状态的设置与获取
获取checkbox是否选中: $("#checkbox").is(":checked"); 获得的值为true或false. 设置checkbox是否选中: ...
- 请问投稿中要求上传的author_SCI投稿过程中主要有哪些状态,持续时间大概多久?...
原标题:SCI投稿过程中主要有哪些状态,持续时间大概多久? 不同出版社旗下SCI杂志的投稿方式.过程以及状态有所区别,但是基本形式大致相同.我们掌握一种杂志的投稿及投稿状态,基本可以以一推百. 不同杂 ...
- jquery input标签中 中checkbox全部选中与全部取消
checkbox 中 checked属性 值 checked 为选中状态 默认值为空 案例代码如下: $(function () {$('#oCheck').click(function () {// ...
- TCP 通信过程中各步骤的状态
状态图 1 状态图 2 对于上面的图 N 多人都知道,它排除和定位网络或系统故障时大有帮助,但是怎样牢牢地将这张图刻在脑中呢?那么你就一定要对这张图的每一个状态,及转换的过程有深刻的认识,不能只停留在 ...
- 对讲业务对讲过程中的几个状态
2019独角兽企业重金招聘Python工程师标准>>> 对基于VOIP业务的对讲有一点了解之后,那我们再来看看要完成一次对讲需要多少状态来表示.大概有下面几种状态: 1.grante ...
- 购物车中 商品的选中状态之后的一些业务逻辑操作
如图,当我们点击 商品的单选框之后 我们要对数据进行处理 总结一下 业务逻辑: 以点击 单个复选框为列 当点击 商品的复选框之后 传入当前商品的id 拿到商品列表,在商品列表中找到所点击商品对象,更改 ...
- layui 数据表格 分页 搜索 checkbox 缓存选中项数据
在做数据表格的时候遇到了很多坑, 今天整理一下方便以后使用. 主要功能是使用数据表格, 做分页,做搜索, 还有checkbox, 支持全选. 当选中一些数据的时候, 数据切换页面数据在切换回来后, ...
- layui 数据表格+分页+搜索+checkbox+缓存选中项数据
在做数据表格的时候遇到了很多坑, 今天整理一下方便以后使用. 主要功能是使用数据表格, 做分页,做搜索, 还有checkbox, 支持全选. 当选中一些数据的时候, 数据切换页面数据在切换回来后, ...
- Android ListView中CheckBox选中状态失效的最佳解决方案
Android ListView中CheckBox选中状态失效: 在ListView中某一项滑到显示区外部的时候,重新划回来时其中的CheckBox的选中状态会重置,本文提出了一种解决方案,操作起来快 ...
- 微信小程序判断某个checkbox是否选中(在非checkboxchange事件中)
做购物车的小程序时,点击已被选中的某个商品的加号时 总计的价格也要跟随商品数量的增加而累加,也就是这个加号的点击事件里,我们要判断当前商品是否是选中状态:我的方法是在checkbox发生改变时,在da ...
最新文章
- 计算机 大学活动 游戏,朝花夕拾”——中国矿业大学计算机学院积极举办“那些年我们一起玩过的游戏”活动...
- DotNetBar12.1新控件——TokenEditor
- Winpcap进行抓包,分析数据包结构并统计IP流量
- 移动端 Web 开发踩坑之旅
- boost::contract模块实现equal的测试程序
- div置于页面底部_网易内部PPT模板有点丑,如何花最少的时间提高页面颜值?
- hdu 4970 killing monster 代代相传刷qq 不用线段树啦~
- pytorch torch.utils.data.Dataset
- Java 使用 POI 对 Excel文件 进行读写操作
- FILEBEAT采集多个目录下日志
- 未来计算机的畅享300,新学期畅享作文300字(通用5篇)
- 阿里云大数据工程师ACP认证例题
- ffmpeg分离视频音频流!
- 在小米实习到SP+转正
- 【开发常用工具】MAC(M1)下安装Windows虚拟机
- 创业7堂课:看准路,选对人,找到钱
- 我对北理FTP联盟的建议
- Jmeter入门教程之配置原件(二)
- Centos最小化安装及配置
- 网络传输中的三张表,MAC地址表、ARP缓存表以及路由表