62,046
社区成员
发帖
与我相关
我的任务
分享
<%@ 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;
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();
}
int CaculateLastRowIndex()
{
int ps = this.GridView1.PageSize;
int pi = this.GridView1.PageIndex;
return (ps * pi + ps > allRows) ? allRows % ps - 1 : ps - 1;
}
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;
}
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]);
Response.Write(((DropDownList)(this.GridView1.Rows[i].FindControl("TestDDL"))).SelectedIndex.ToString());
}
}
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
}
}
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;
using System.Text;
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();
}
int CaculateLastRowIndex()
{
int ps = this.GridView1.PageSize;
int pi = this.GridView1.PageIndex;
return (ps * pi + ps > allRows) ? allRows % ps - 1 : ps - 1;
}
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;
}
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]);
Response.Write(((DropDownList)(this.GridView1.Rows[i].FindControl("TestDDL"))).SelectedIndex.ToString());
}
}
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
}
}
[code=HTML]
<%@ 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>
DropDownList ss=(DropDownList)(this.GridView1.Rows[i].Cells[2].FindControl("dpl"));
ss.SelectedIndex=int.Parse(selectedIndex[i]);
private void GetCacheDropDownListSelectedIndex(int pageIndex,int pageCount,int pageSize)
{
int allRowCounts=pageSize*pageCount;
int currentPageFirstRowIndex=(pageIndex==0)?pageSize*pageIndex:pageSize*pageIndex-1;
int currentPageLastRowIndex=(pageCount>=currentPageFirstRowIndex+pageCount)?(allRowCount-1):(currentPageFirstRowIndex+pageCount-1);
string cacheKey="page"+pageIndex.ToString();
string dpl_selectedIndexStr=Cache[cacheKey].ToString();
string[] selectedIndex=dpl_selectIndexStr.Split('|');
for(int i=currentPageFirstRowIndex;i<=currentPageLastRowIndex;i++)
{
(DropDownList)(this.GridView1.Rows[i].FindControl("dpl")).SelectedIndex=int.Parse(selectdIndex[i]);
}
}
private void CachDropDownListSelectedIndex(int pageIndex,int pageCount,int pageSize)
{
string dpl_selectIndexStr=string.Empty;
int allRowCounts=pageSize*pageCount;
int currentPageFirstRowIndex=pageSize*pageIndex;
int currentPageLastRowIndex=(pageCount>=currentPageFirstRowIndex+pageCount)?allRowCounts:(currentPageFirstRowIndex+pageCount);
string cacheKey="page"+pageIndex.ToString();
for(int i=currentPageFirstRowIndex;i<=currentPageLastRowIndex;i++)
{
dpl_selectIndexStr+=(DropDownList)(this.GridView1.Rows[i].FindControl("dpl"));
dpl_selectIndexStr=dpl_selectIndexStr.Substring(0,dpl_selectIndexStr.Length-2);
}
Cache[cacheKey]=dpl_selectIndexStr;
}
//获取每一页的保存的SelectedIndex并恢复原来的DropDownList的选项
private void GetCacheDropDownListSelectedIndex(int pageIndex,int pageCount,int pageSize)
{
int allRowCounts=pageSize*pageCount;
int currentPageFirstRowIndex=(pageIndex==0)?pageSize*pageIndex:pageSize*pageIndex-1;
int currentPageLastRowIndex=(pageCount>=currentPageFirstRowIndex+pageCount)?(allRowCount-1):(currentPageFirstRowIndex+pageCount-1);
string cacheKey="page"+pageIndex.ToString();
string dpl_selectedIndexStr=Cache[cacheKey].ToString();
string[] selectedIndex=dpl_selectIndexStr.Split('|');
for(int i=currentPageFirstRowIndex;i<=currentPageLastRowIndex;i++)
{
this.GridView1.Rows[i].FindControl("dpl").SelectedIndex=int.Parse(selectdIndex[i]);
}
}