How to change asp:ListView DataPager page from codebehind (c#)

2.6k views Asked by At

Quite simple question, but I am having loads of issues with it.

    protected void restorePagerNumber()
    {
        if (Session["PageNumber"] != null)
        {
            System.Diagnostics.Debug.Write(Session["PageNumber"]);
            DataPager pager = searchListView.FindControl("searchDataPager") as DataPager;
            pager.SetPageProperties((int)Session["PageNumber"] * pager.PageSize, pager.MaximumRows, false);
        }
    }

Thats what I currently have, I tried to use it before databind, after data bind, none of them seem to work. Can I actually change pager value after creating new object? Doesnt sound logical, but I cannot access datapager without that. Is there another way to access dataPager that is in a listView and maybe another way to set its page number.

Cheers

1

There are 1 answers

1
Jeff Mergler On

I found a scenario that is similar to yours (https://web.archive.org/web/20210125144848/http://www.4guysfromrolla.com/articles/021308-1.aspx) and I verified that the sample application works calling SetPageProperties() at run-time.

Be sure to change the last "databind" argument in your SetPageProperties call to from False to True:

pager.SetPageProperties((int)Session["PageNumber"] * pager.PageSize, pager.MaximumRows, true);

then make sure you're calling restorePagerNumber at PageLoad

protected void Page_Load(object sender, System.EventArgs e)
{
    if (!Page.IsPostBack) {
        restorePagerNumber();
    }
}

Hope that helps.