Tutorial :ASP.NET data binding on postback


Working on an ASP.NET 4.0 project, which uses user controls to dynamically generate a form based on definitions stored in the database. One form field would look like this:

<usc:RefControl ID="ctrlUser1"       ReferenceFieldId='product.user1'       ValidationFormat="^\d+\.?\d{0,2}$"      runat="server"/>  

Behind the scenes the control emits a RegularExpressionValidator based on the RefControl.ValidationFormat property.

This works fine, however the problem is that this architecture only allows us to validate with regexes. Now I need to add date format validation, based on the user's preferences (which aren't dependent on their UICulture).

Reluctant to refactor the entire user control setup, I need to be able to pass a on-the-fly regex pattern to the ValidationFormat property. Currently I'm using a data binding expression to bind the property to a page instance method:

<usc:RefControl ID="ctrlUser2"       ReferenceFieldId='product.user2'       ValidationFormat="<%# GetUserDateValidationFormat()%>"       runat="server"/>   

Code behind:

/// <summary>  /// Returns a regular expression that validates the current user's date format  /// </summary>  public string GetUserDateValidationFormat()   {      //...  }  

Works okay on first page load, but on subsequent postbacks the validation doesn't work. I think the issue is that the data binding expression doesn't evaluate at all, but I'm not sure I understand why. I'm calling Page.DataBind() in Page_Init whether Page.IsPostBack or not, so shouldn't this work?

If you see I'm barking up the wrong tree, any alternative solutions to the same problem are also welcome.


Managed to solve this problem. The issue was with the way ASP.NET page life cycle invokes the user control's events before the page's own events. The control values were being initialized before the data binding on the page could happen.

Because I still need to do the control initialization before Page_Load to subscribe the controls to viewstate, I simply moved the initialization logic to the Page.InitComplete event so the call to Page.DataBind() could get called first.

    protected void Page_Init(object sender, EventArgs e)      {          Page.InitComplete += new EventHandler(Page_InitComplete);      }  

So the event tree becomes

  1. User Control Page_Init => Hook InitComplete handler
  2. Page (aspx) Page_Init => Bind data
  3. User Control Page_InitComplete => Initialize the control


Couldn't you just set the property in Page_Load()?

public void Page_Load(...)  {      ctrlUser1.ValidationFormat = GetUserDateValidationFormat();      // do whatever stuff you do  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »