使用DataPager时,GridView会丢失有关回发的数据

时间:2012-05-10 08:41:21

标签: c# asp.net gridview viewstate datapager

我有一个CustomGridView(扩展GridView)和一个DataPager,所有在.net 3.5中工作正常但在.net 4中没有。问题是当在第一个页面上发生回发时,网格丢失了视图状态。这是一个大型项目,并且在每个回发上进行数据绑定不是一个选项,因为它需要更新的代码(我们开始但我们只创建了更多的错误)。

我已经测试了内置的DataPager webcontrol,并且我已经使用相同的结果制作了自己的内容。我们知道我们应该为网格使用内置的寻呼机,但这种解决方案是不可能的,因为要更新的代码太多了。

问题出现在使用CustomGridView的所有页面上,并且在网格页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);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将DataBind(0);置于网络表单的OnPreRender()方法

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    DataBind(0);
}