Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Windows Azure, PHP, ...

About the author

Maarten Balliauw is currently employed as .NET Technical Consultant at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
View Maarten Balliauw's MVP profile

Search

Latest Twitter

    Follow me on Twitter...

    Archive

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2012


    ASP.NET DataPager not paging after first PostBack?

    A few posts ago, I mentioned that I am currently giving a classroom training on ASP.NET. People attending are currently working on a project I gave them, and today one of them came up to me with a strange problem...

    Here's the situation: in VS 2008, a web page was created containing 2 controls: a DataList and a DataPager. This DataPager serves as the paging control for the DataList. Databinding is done in the codebehind:

    protected void Page_Load(object sender, EventArgs e) {
        ListView1.DataSource = NorthwindDataSource;
        ListView1.DataBind();
    }

    This works perfectly! When the page is rendered in a brwoser window, data is shown in the DataList control. Now, when testing the DataPager, something strange happens: when a page number is clicked, ASP.NET will process a PostBack, rendering... the same page as before! Clicking the DataPager again is the only way to really go to a different page in the result set.

    Let's have a look at the ASP.NET page lifecycle... The page Load event is actually not the best place to call the DataBind() method. PreRender is a better place to call DataBind():

    protected void Page_Load(object sender, EventArgs e) {
        ListView1.DataSource = NorthwindDataSource;
    }

    protected void Page_Render(object sender, EventArgs e) {
        ListView1.DataBind();
    }

    kick it on DotNetKicks.com


    Categories: ASP.NET | C# | General | Software

    Comments (4) -

    Brad United States |

    Wednesday, February 06, 2008 2:21 AM

    Brad

    What if you need to doing your databind after several methods in the page load have already run.

    There has to be a fix for this somehow...

    maartenba Belgium |

    Wednesday, February 06, 2008 8:58 AM

    maartenba

    This is simply due to the ASP.NET page lifecycle... Of course you can work around this, but I fear that might lead to frustration.

    brad United States |

    Saturday, February 09, 2008 7:59 AM

    brad

    How can you work around this?

    Jonathan Brazil |

    Saturday, February 14, 2009 9:53 PM

    Jonathan

    check the end of this page, I found the solution there
    nayyeri.net/.../
    if you have problems to see it, here is the solution:

    aspx:
    <asp:ListView runat="server" ID="rptProds" OnPagePropertiesChanging="listItems_PagePropertiesChanging">

    code behind:
        protected void listItems_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) {

          this.pagingProds.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

          BindYourDataAgain();

        }

    Comments are closed