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