开发者

When to databind data-bound controls?

开发者 https://www.devze.com 2022-12-25 13:05 出处:网络
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

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.


I prefer to do all databinding in code where I can dictate when it will occur similar to how you are doing it in your Page_Load example or on a click etc...

This way if you need manipulate other controls (not related to the control you are databinding) AFTER the databind has occurred you have the ability to do so without having to resort to doing it in the events like OnDataBinding or OnDataBound. I typically use those events to affect the control being bound specifically and not other controls.

I also prefer a finer control and all the auto wire up features of these controls isn't always flexible. I don't like having half my code do it one way and then the other half another (eg. some in code, some in the aspx to auto wire up) so I just do it all via code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号