GridView和CheckBox连用,实现全选,分页,并且分页之后选择的复选框的状态依然存在
来源:互联网 发布:qq imap服务器 端口 编辑:程序博客网 时间:2024/05/21 21:37
页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>
</head>
<body>
<form id="form1" runat="server">
<div>
按产品名称模糊查询: <asp:TextBox ID="pname" runat="server"></asp:TextBox> <asp:Button ID="chaxun" Text="查询" runat="server" OnClick="chaxun_Click" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="100%" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" OnPageIndexChanged="GridView1_PageIndexChanged" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="全选">
<ItemTemplate>
<asp:CheckBox ID="cb" runat ="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="quanxuan" runat="server" Text="全选" OnCheckedChanged="quanxuan_CheckedChanged" AutoPostBack="true"/>
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductID" HeaderText="产品编号" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="产品名称" SortExpression="ProductName" />
<asp:BoundField DataField="UnitPrice" HeaderText="单价" SortExpression="UnitPrice" DataFormatString="{0:c}元" />
</Columns>
<FooterStyle BackColor="Teal" />
<PagerStyle BackColor="Teal" />
<HeaderStyle BackColor="Teal" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]"></asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products] WHERE ([ProductName] LIKE '%' + @ProductName + '%')">
<SelectParameters>
<asp:FormParameter FormField="pname" Name="ProductName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
服务器代码:
using System;
using System.Data;
using System.Configuration;
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;
using System.Web.UI.MobileControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void quanxuan_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox cb = gr.FindControl("cb") as CheckBox;
if (cb.Checked == false)
{
cb.Checked = true;
}
else
{
cb.Checked = false;
}
}
}
protected void chaxun_Click(object sender, EventArgs e)
{
Session["datasourse"] = "SqlDataSource2";
bind();
}
protected void bind()
{
if (Session["datasourse"] == null)
{
GridView1.DataSourceID = "SqlDataSource1";
}
else
{
GridView1.DataSourceID = (string)Session["datasourse"];
}
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (Session["data"] == null)
{
List datanew = new List();
Session["data"] = datanew;
}
List data = (List)Session["data"];
GridView1.PageIndex = e.NewPageIndex;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cb = GridView1.Rows[i].FindControl("cb") as CheckBox;
if (cb.Checked)
{
string pid = GridView1.DataKeys[i].Value.ToString();
if (!data.Items.Contains(pid)) //判断list集合里是否包含这个元素
{
data.Items.Add(pid);
}
}
else //如果取消选择,那么就从list集合利去除这个元素
{
string pid = GridView1.DataKeys[i].Value.ToString();
data.Items.Remove(pid);
}
}
Session["data"] = data;
bind();
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Session["data"] != null)
{
List data = (List)Session["data"];
if (data.Items.Count > 0)
{
for (int j = 0; j < data.Items.Count; j++)
{
string ProductId = e.Row.Cells[1].Text;
if (ProductId == data.Items[j].Value.ToString())
{
CheckBox cb = ((CheckBox)e.Row.FindControl("cb"));
cb.Checked = true;
}
}
}
}
}
}
}
代码不是很难的,所以没有写注释
- GridView和CheckBox连用,实现全选,分页,并且分页之后选择的复选框的状态依然存在
- checkbox复选框的全选和全不选
- gridview分页保留复选框状态
- GridView控件中添加CheckBox控件,并且实现选择、全选和反选
- asp分页,复选框全选,选中的全删
- GridView分页+保持checkbox状态
- GridView的定义存储过程分页和全选删除
- 用jquery实现复选框checkbox的全选
- android checkBox (复选框)简单例子,全选的实现
- jquery实现checkbox复选框的全选反选
- jquery实现复选框(checkbox)的全选与反选
- HTML 复选框 checkbox 的 JavaScript 的全选和全反选
- 在GridView分页时,维护CheckBoxes控件的选择状态
- GridView实现的分页
- GridView分页的实现
- GridView分页的实现
- GridView分页的实现
- jq实现复选框全选和反全选以及子复选框的状态改变父复选框状态的方法
- 名称以无效字符开头问题-终于解决了
- 下面的程序的功能是将"mov ax,4c00h"之前的指令复制到内容0:200处,补全程序
- J2ME学习笔记(基础3)
- 谈自己对如何构造网络引擎粗浅的看法
- linux系统安装之后时间渐渐变慢怎么办
- GridView和CheckBox连用,实现全选,分页,并且分页之后选择的复选框的状态依然存在
- 安装gcc-4.1.0
- 排序和算法总结
- Citrix XenCenterWeb (XSS/SQL/RCE) Multiple Remote Vulnerabilitiesv
- J2ME学习笔记(基础4)
- WebApplicationContext初始化
- 链接错误LNK2001
- SQL SERVER 和EXCEL转换
- 怎么做外链