views:

47

answers:

2

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:

  1. On the property, I could add a code block that would call this.DataBind().
  2. I could add the code by overriding the virtual method DataBind.
  3. 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.

A: 

Your control should not depend, in any way, on the page in which it is embedded. Instead, have an "AutoUpdate" property on the control, and have the page set it. The page should also set properties telling the control how to update when the AutoUpdate property is set.

John Saunders
@Saunders This is what I'm doing. I will add clarification.
Curtis White
@Curtis: first of all, use the start of the username, like "@John". Second, your control should _still_ not be loading the data on its own. Look at how a DataGrid control does it. Also, if you are not binding to data, then don't use the DataBind method. Simply override OnPreRender or maybe Render if the output of the control differs based on this property. If the _data_ differs based on the property, then, again, the page should say which data to use, not the control.
John Saunders
A: 

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.

Dr. Wily's Apprentice
@DrWily Thanks, this makes sense. I will wait to see if I get any more suggestions. Some of the complexity in this comes from the fact I'm storing state data using the ViewState but only the Id but for convenience I'm also storing an object in the property thus when the Id changes the other property should change. But the first time, I am actually loading the Id from the full object. So, I don't really want to load the full object unless required. I see now it is a bit of a mess.
Curtis White