使用GridView分页时,保持内嵌控件状态的技巧。(散分100)

liuyeede 2009-03-10 02:22:47
当时用GridView中使用分页导航到其他页,如果其中内嵌其他控件,比如TextBox、DropDwonList、CheckBox等控件,先前页面的内嵌控件的状态不会保存。当我们返回到先前页面时,仍然会被初始化为默认状态。为了保持我们操作过的内嵌控件的状态。我做了下面的代码,请大家拍砖。如果各位有什么好的方案,可以贴上来,共同学习,共同进步。

<%@ 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:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
PagerSettings-Mode="NumericFirstLast" AllowPaging="True"
OnPageIndexChanging="PageIndexChanging" OnPageIndexChanged="PageIndexChanged">

<Columns>
<asp:BoundField DataField="CompanyName" HeaderText="公司名称" />
<asp:BoundField DataField="ContactName" HeaderText="联系人" />
<asp:BoundField DataField="Phone" HeaderText="联系电话" />
<asp:TemplateField HeaderText="测试DropDownList" ShowHeader= "true">
<ItemTemplate>
<asp:DropDownList ID="TestDDL" runat="server">
<asp:ListItem Value="1" Text="One" />
<asp:ListItem Value="2" Text="Two"/>
<asp:ListItem Value="3" Text="Three" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// 本例以Cache暂存数据,但在实际引用时,建议选用Cookie.
/// </summary>
public partial class _Default : System.Web.UI.Page
{
public static string sqlStr = "Select CompanyName,ContactName,Phone From Customers";
public static int allRows = 0;//获取取到数据的总行数;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}

private void bind()
{
string connStr = ConfigurationManager.ConnectionStrings["TestSqlConnection"].ConnectionString;

SqlConnection conn = new SqlConnection(connStr);
DataSet dt = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn);
try
{
conn.Open();
da.Fill(dt, "Customers");
allRows=dt.Tables["Customers"].Rows.Count;
}
catch
{
throw new ApplicationException("数据库错误");
}
finally
{
conn.Close();
}

this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
/// <summary>
/// 判断是否为最后一页,最后一页的行数可能小于指定的PageSize;
/// </summary>
/// <returns></returns>
int CaculateLastRowIndex()
{
int ps = this.GridView1.PageSize;
int pi = this.GridView1.PageIndex;
return (ps * pi + ps > allRows) ? allRows % ps - 1 : ps - 1;
}
/// <summary>
/// 将DropDrownList的选择项拼接为字符串,保存到Cache中。以"Page"和页面索引PageIndex拼接后的字符串
/// 作为Cache的存取键。
/// </summary>
private void CachDropDownListSelectIndex( )
{
string dpl_selectIndexStr =string.Empty;
string cacheKey = "Page" + GridView1.PageIndex.ToString();
for (int i = 0; i <= CaculateLastRowIndex(); i++)
{
dpl_selectIndexStr = dpl_selectIndexStr+((DropDownList)(this.GridView1.Rows[i].FindControl("TestDDL"))).SelectedIndex.ToString();
dpl_selectIndexStr = dpl_selectIndexStr + "|";
}
dpl_selectIndexStr = dpl_selectIndexStr.Substring(0, dpl_selectIndexStr.Length - 1);
Cache[cacheKey] = dpl_selectIndexStr;

}
/// <summary>
/// 分解保存的Cache中保存的内嵌控件的状态数据,并恢复。
/// </summary>
private void GetCachedDropDownListSelectedIndex( )
{
string cacheKey = "Page" + GridView1.PageIndex.ToString();
if ((Cache[cacheKey] == null)||(Cache[cacheKey].ToString()==string.Empty))
{
return;
}
string dpl_selectIndexStr = Cache[cacheKey].ToString();
string[] selectedIndex = dpl_selectIndexStr.Split('|');

for (int i = 0; i <= CaculateLastRowIndex(); i++)
{
((DropDownList)(this.GridView1.Rows[i].FindControl("TestDDL"))).SelectedIndex = int.Parse(selectedIndex[i]);

}

}

protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.CachDropDownListSelectIndex();//换页以前的PageIndex
GridView1.PageIndex = e.NewPageIndex;
bind();

}

protected void PageIndexChanged(object sender, EventArgs e)
{
this.GetCachedDropDownListSelectedIndex(); //换页以后的PageIndex
}
}

...全文
705 65 打赏 收藏 转发到动态 举报
写回复
用AI写文章
65 条回复
切换为时间正序
请发表友善的回复…
发表回复
BEN999 2010-04-12
  • 打赏
  • 举报
回复
努力学习中!
wp19861223 2009-04-02
  • 打赏
  • 举报
回复
收藏
lonelySurvive 2009-03-18
  • 打赏
  • 举报
回复
学习了
yh7272hy 2009-03-11
  • 打赏
  • 举报
回复
学习
liuyeede 2009-03-11
  • 打赏
  • 举报
回复
blc2003 2009-03-10
  • 打赏
  • 举报
回复
MARK
amandag 2009-03-10
  • 打赏
  • 举报
回复
学习
jiang_jiajia10 2009-03-10
  • 打赏
  • 举报
回复
学习了
liberpc 2009-03-10
  • 打赏
  • 举报
回复
关注
liuyeede 2009-03-10
  • 打赏
  • 举报
回复
多提方案多给分!!
wuyq11 2009-03-10
  • 打赏
  • 举报
回复
保存到hiddenfield或cache都可。
ilovew521527 2009-03-10
  • 打赏
  • 举报
回复
汗。。。
limpid_123 2009-03-10
  • 打赏
  • 举报
回复
我是来接分的:)
jianice 2009-03-10
  • 打赏
  • 举报
回复
我也是来接分的,哈哈
amwsse21q 2009-03-10
  • 打赏
  • 举报
回复
我也是来接分的
lsd123 2009-03-10
  • 打赏
  • 举报
回复
Dhoopu 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 findcaiyzh 的回复:]
我是来接分的:)
[/Quote]
sxmonsy 2009-03-10
  • 打赏
  • 举报
回复
我也是来接分的
xsm545 2009-03-10
  • 打赏
  • 举报
回复
学习..收藏了
wanghui0380 2009-03-10
  • 打赏
  • 举报
回复
cache可是全局公用滴

你这么放到不如放到viewstate里,天知道多人并发操作下你的cache会不会混乱
加载更多回复(45)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧