Dropdown in ASCX, when to databind?

  • Thread starter Thread starter cmay
  • Start date Start date
C

cmay

A common situation I come across is an ASCX control that being used as
a lookup control.

The ascx might be called "StatusLookup" or something, and it consists
of 1 dropdown list that populates itself from cache/database list.

If you databind on the Init event, then you are databinding every time,
and not making any use of the viewstate.

If you databind on the Load event, you can see if the control has
already been loaded by the viewstate, but if you have code in your
pages Load event, that can run before the Load event on your ASCX
control is fired.

So, you can end up with a situation where you PAGE load method is
trying to set the value of the dropdown in the ASCX, but the ASCX has
not yet databound.

I have always written some code to work around this situation, using a
series of checks to see if items have been added to the dropdown or
not, but I am wondering if there is a better pattern to be followed
when doing something like this.
 
if you turn off viewstate (like I do to save bandidth and make paes
faster) you databind in oninit and its simple.

-- bruce
 
bruce said:
if you turn off viewstate (like I do to save bandidth and make paes
faster) you databind in oninit and its simple.

-- bruce


Are you talking about turning off viewstate at the control, or for the
whole page?

If you have the viewstate totally off, that would cause you to have to
set the selected value of the control every postback.
 
Back
Top