ASP.Net : DataPager Control always a step behind with paging

后端 未结 6 847
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 16:28

Take the following example...a page with a ListView and a DataPager used for paging the data of the ListView:

Code Behind:

相关标签:
6条回答
  • 2020-12-14 17:10

    Alternatively, if you are building a User control only containing the ListView, you can simply point the pager event handler to the Page_Load method, since the Page_Load method isn't running anything else:

    <asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10"
    OnPreRender="Page_Load">
    
    0 讨论(0)
  • 2020-12-14 17:12

    Following works perfect for me.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles   Me.Load
    Dim ds As DataSet
    ds = SQLHELPER.ExecuteDataSet(CommandType.StoredProcedure, "sp_Locations")
    rs.EnableViewState = False
    rs.DataSource = ds
    rs.DataBind()
    End Sub
    
    Protected Sub rs_PagePropertiesChanging(ByVal sender As Object, ByVal e As    PagePropertiesChangingEventArgs)
    'set current page startindex, max rows and rebind to false
    Pager.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
    'rebind List View
    rs.DataBind()
    End Sub
    
    <asp:ListView ID="rs" runat="server" onpagepropertieschanging="rs_PagePropertiesChanging">
    
    0 讨论(0)
  • 2020-12-14 17:15

    You are missing the OnPreRender event on the datapager!

    0 讨论(0)
  • 2020-12-14 17:19

    I ran into this same problem, but binding everytime on datapager prerender was not an option for me. Instead, I was able to accomplish much the same thing by binding only when the paging occurred. This solution can be used as an alternative to the prerender solution by Andreas. The following worked for me:

    By attaching to the ListView's PagePropertiesChanged event, I was able to correct the paging issue without having to bind on every prerender of the data pager.

    NOTE: Most of the data pager properties are setup in a skin file, which is why they are not in the markup.

    Markup:

    <asp:DataPager ID="ListPager" runat="server" PagedControlID="MyList" />
    <asp:ListView ID="MyList" runat="server">
        <% //LayoutTemplate and ItemTemplate removed for the example %>
    </asp:ListView>
    

    Code Behind:

    protected void Page_Load(object sender, EventArgs e) {
       MyList.PagePropertiesChanged += new EventHandler(MyList_PagePropertiesChanged);
    }
    
    /// <summary>
    /// Handles the situation where the page properties have changed.  Rebind the data
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void MyList_PagePropertiesChanged(object sender, EventArgs e) {
       MyList.DataSource = GetSomeList();
       MyList.DataBind();
    }
    
    0 讨论(0)
  • 2020-12-14 17:21

    Solution

    The problem is due to the binding occuring on the Page_Load event.

    For this to work as expected, the binding needs to happen in the DataPager's OnPreRender event, not in the Page_Load.

    Source:

    <asp:DataPager ID="ListPager" PagedControlID="MyList" runat="server" PageSize="10"
        OnPreRender="ListPager_PreRender">
    
    <Fields>
            <asp:NumericPagerField  />
        </Fields>
    </asp:DataPager>
    

    Code Behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        //Binding code moved from Page_Load
        //to the ListView's PreRender event
    }
    
    protected void ListPager_PreRender(object sender, EventArgs e)
    {
        MyList.DataSource = GetSomeList();
        MyList.DataBind();    
    }
    
    0 讨论(0)
  • 2020-12-14 17:25

    in the page load you should put the code between if (!IsPostBack) { }

    It will solve your problem.

    0 讨论(0)
提交回复
热议问题