GridView 翻页记住选项

Posted 御不凡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GridView 翻页记住选项相关的知识,希望对你有一定的参考价值。

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="ObjectDataSource1"
PageSize="5" onpageindexchanging="GridView1_PageIndexChanging"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="ckbSelAll" runat="server" Text="" OnCheckedChanged="cbSelAll_CheckedChanged" AutoPostBack="true" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="ckbxSel" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="xm" HeaderText="xm" SortExpression="xm" />
</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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["selectItems"] = "";
}
}

protected void SelectAll()
{
CheckBox cb1 = GridView1.HeaderRow.FindControl("ckbSelAll") as CheckBox;
string strOldSelect = ViewState["selectItems"].ToString();
string[] oldSelect = strOldSelect.Split(new char[] { ‘,‘ });
List<string> listSelect = oldSelect.ToList();
string nowId;
if (cb1.Checked)//选中
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb2 = GridView1.Rows[i].FindControl("ckbxSel") as CheckBox;
cb2.Checked = true;

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

nowId = GridView1.Rows[i].Cells[1].Text;
if (true == listSelect.Contains(nowId))
{
listSelect.Remove(nowId);
}
}
}
}

protected void cbSelAll_CheckedChanged(object sender, EventArgs e)
{
this.SelectAll();
}

//翻页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string strSelectLian = ViewState["selectItems"].ToString();
string nowId;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = GridView1.Rows[i].FindControl("ckbxSel") as CheckBox;
nowId = GridView1.Rows[i].Cells[1].Text;
if (true == chk.Checked)
{
strSelectLian += "," + nowId;
}
}
strSelectLian = strSelectLian.Trim(‘,‘);
ViewState["selectItems"] = strSelectLian;

//翻页
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSourceID = "ObjectDataSource1";
GridView1.DataBind();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//绑定原来选项
if (e.Row.RowIndex != -1)//注意加上这句
{
string strSelectLian = ViewState["selectItems"].ToString();
string[] strSelectFen = strSelectLian.Split(new char[] { ‘,‘ });
string nowId = e.Row.Cells[1].Text;
CheckBox chk = e.Row.FindControl("ckbxSel") as CheckBox;
for (int j = 0; j < strSelectFen.Length; j++)
{
if (nowId == strSelectFen[j])
{
chk.Checked = true;
}
}
}
}

}

 

以上是关于GridView 翻页记住选项的主要内容,如果未能解决你的问题,请参考以下文章

JQuery tabcontrol:加载gridview

ListView 和 GridView 在选项卡切换之间被清除

如何在 ASP.Net Gridview 中添加“确认删除”选项?

gridview编辑模板中没有itemtemplate

Android 实现仿美团首页左右滑动GridView+ViewPager菜单栏的功能

Bootstrap 在 ASP.NET 中使用 gridview 分页保存当前选项卡