I’ve a custom property on a user control which has multiple state/modes. If this property is set in the parent page: I would like for my control to update automatically. Using the property in the page load does not work because it is not initiated.
I can imagine 3 methods to do this:
- On the property, I could add a code block that would call this.DataBind().
- I could add the code by overriding the virtual method DataBind.
- I could create a public proprietary Update method.
I would like any input into what is the best practice in general. More to the point, I’ve chosen to override the virtual method DataBind. My pseudo code is as such:
public override void DataBind()
{
if (SpecialMode)
{
.. load from database
}
base.DataBind()
}
I’m interested in the ordering of the base.DataBind(). I’ve seen it typically placed first but after I load the data from the database: I will need to databind to get the data to display.
Any input into these considerations will be greatly appreciated.
To be clear:
This control is a Poll widget. It will typically search and load the poll to display from the Page_Load event. But, it also has a reports mode which allows the page in which the control is embedded to change the Id of the poll to display. This property will not be initiated in Page_Load. Okay, part of this mess is that I’ve a property for an object, and I’ve also a duplicated property for the ViewState but only the Id.
You could have your user control handle the automatic databinding in its PreRender event. When the Id property is changed (or whatever property it is that triggers the automatic databinding) you could set a flag within the control that is later checked in PreRender to indicate that data needs to be loaded.
Alternatively, instead of setting a flag you could possibly clear any data that you’ve already loaded from ViewState and implement logic in PreRender to say “If there is no data, then load it”.
This avoids potentially loading data multiple times within a single request due situations where there happens to be code that sets the Id property multiple times.