Welcome to Windows Communication Foundation (WCF)
Top Tasks :

WCF Community Bloggers

Thursday, May 29, 2008 - Posts

  • When does Declarative Data Binding Happen?

    I try to use declarative data binding (let's call it DDB for short) wherever I can in my ASP.NET apps, but there's one pain point I've run into in the past. It has to do with lists. Let's say you want to populate a list box or drop down list of choices by calling a static method - you can wire up an ObjectDataSource to do that. But then in your Page's Load event, you may want to look up a record and select one of those choices based on data from that record. Maybe the user chose the item in the past and you're looking in the user's profile to recall her choice. Well, you'll quickly find that Load fires before declarative data binding has occurred. You'll probably discover this like I did when you get an error because there are no items in the list to select from. In the past I've simply avoided declarative data binding when I ran into this problem and used programmatic data binding instead (control.DataSource=collection; control.DataBind; control.SelectedIndex=whatever;). But today I talked with Fritz Onion , ASP.NET guru, and we figured out when declarative data binding actually occurs. We tested LoadComplete, and that wasn't late enough. Then we figured certainly DDB would happen before PreRender. No such luck. So finally Fritz suggested testing out PreRenderComplete, and that worked like a charm. So by the time PreRenderComplete has fired, DDB has already taken place and you can safely make list selections for lists that have been populated via DDB. My current solution looks like this: protected void Page_Load(object sender, EventArgs args) { if (!IsPostBack) PreRenderComplete += PopulateControlsFromUserData; } void PopulateControlsFromUserData(object sender, EventArgs args) { // DDB has already happened, so party on! } I hope this helps someone else who also searches for "when does declarative data binding happen", which didn't used to bring up any results :-) Read More...

Copyright © 2006 Microsoft Corporation. All Rights Reserved. | Terms of Use | Privacy Statement | Contact Us