62,074
社区成员
发帖
与我相关
我的任务
分享
<%@ 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
}
}