我有一个 CustomGridView(扩展 GridView)和一个 DataPager,在 .net 3.5 中一切正常,但在 .net 4 中却不行。问题是当回发发生在第一个页面以外的页面上时,网格的视图状态会丢失。这是一个大型项目,并且在每次回发时都进行数据绑定不是一种选择,因为要更新的代码太多(我们开始了,但我们只创建了更多错误)。
我已经测试了内置的 DataPager webcontrol,并且我自己制作了相同的结果。我们知道我们应该为网格使用内置寻呼机,但该解决方案是不可能的,因为要更新的代码太多。
问题出现在使用 CustomGridView 的所有页面上,并且在 gridpage 1 上一切正常
这是一个已知问题,是否有解决方法?
我给你一个简单项目的代码,但出现了同样的问题(在 .net 3.5 中工作,但不是在 .net 4 中工作):
WebForm1.aspx:
<cc1:SimpleCustomGridView ID="CustomGridView1" runat="server"
AutoGenerateColumns="false" PageSize="4" AllowPaging="true"
onpageindexchanging="CustomGridView1_PageIndexChanging"
onrowcommand="CustomGridView1_RowCommand" EnableViewState="true" ViewStateMode="Enabled" Visible="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkButton" runat="server" Text='<%#Eval("Name") %>' CommandName="lnkClick" CommandArgument='<%#Eval("Id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</cc1:SimpleCustomGridView>
<br />
<uc:DataPager ID="DataPager" runat="server" CustomGridViewID="CustomGridView1" />
WebForm1.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBind(0);
}
}
private void DataBind(int pageIndex)
{
int rowIndex = pageIndex * CustomGridView1.PageSize;
int pageSize = CustomGridView1.PageSize;
int total;
List<DataContainer> list = DataContainer.GetData(rowIndex, pageSize, out total);
CustomGridView1.RowCount = total;
CustomGridView1.DataSource = list;
CustomGridView1.DataBind();
}
protected void CustomGridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (e.NewPageIndex > -1)
{
CustomGridView1.PageIndex = e.NewPageIndex;
DataBind(e.NewPageIndex);
}
}
protected void CustomGridView1_RowCommand(object sender, GridViewCommandEventArgs e) { }
SimpleCustomGridView.cs:
public class SimpleCustomGridView : GridView
{
public override int PageIndex
{
get
{
if (ViewState[this.ClientID + "PAGEINDEX"] == null)
ViewState[this.ClientID + "PAGEINDEX"] = 0;
return (int)ViewState[this.ClientID + "PAGEINDEX"];
}
set
{
base.PageIndex = value;
ViewState[this.ClientID + "PAGEINDEX"] = value;
}
}
public int RowCount
{
set
{
HttpContext.Current.Session[this.ClientID + "TOTALPAGECOUNT"] = value;
}
get
{
return HttpContext.Current.Session[this.ClientID + "TOTALPAGECOUNT"] == null ? 0 :
Convert.ToInt32(HttpContext.Current.Session[this.ClientID + "TOTALPAGECOUNT"]);
}
}
public void GotoPage(int pageIndex)
{
GridViewPageEventArgs args = new GridViewPageEventArgs(pageIndex);
OnPageIndexChanging(args);
}
}
CustomGridViewpager.ascx.cs:
public partial class CustomGridViewpager : UserControl
{
public string CustomGridViewID { get; set; }
private SimpleCustomGridView _gridView;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
_gridView = (SimpleCustomGridView)Parent.FindControl(CustomGridViewID);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
int totalRows = _gridView.RowCount;
int pageIndex = _gridView.PageIndex;
int pageSize = _gridView.PageSize;
int totalPages = (int)Math.Ceiling(totalRows / (double)pageSize);
StringBuilder sb = new StringBuilder();
sb.Append(pageIndex + 1);
sb.Append("/");
sb.Append(totalPages);
PageIndexLabel.Text = sb.ToString();
PrevPageButton.Visible = pageIndex > 0;
NextPageButton.Visible = pageIndex < totalPages - 1;
}
protected void NextPageButton_Click(object sender, EventArgs e)
{
_gridView.GotoPage(_gridView.PageIndex + 1);
}
protected void PrevPageButton_Click(object sender, EventArgs e)
{
_gridView.GotoPage(_gridView.PageIndex - 1);
}
}