When to databind data-bound controls?

Posted by sanjuro on Stack Overflow See other posts from Stack Overflow or by sanjuro
Published on 2010-03-29T12:32:17Z Indexed on 2010/03/29 12:33 UTC
Read the original article Hit count: 276

Hi, i have little dilemma, i often use data-bound controls such as Gridview in conjunction with ObjectDataSource. But i have two possible options when can i bind data to Gridview. The first is that i set datasourceid of gridview in aspx file and databind occurs in a moment before PreRender event occurs or i can set datasource in Page_Load event and databinding occurs immediately, something like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        testGridView.DataSource = testObjectDataSource.Select();
        testGridView.DataBind();
    }
}

I think that in second approach i have more control above databinding. But how it is in real programming life? Which of the above two options is commonly used? Or is there some third option how can i bind data to data-bound control? Thanks for your opinions from real-life experiences.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET