Dropdown in ASCX, when to databind?

Discussion in 'ASP .Net' started by cmay, Dec 11, 2006.

  1. cmay

    cmay Guest

    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.
    cmay, Dec 11, 2006
    #1
    1. Advertising

  2. cmay

    bruce barker Guest

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

    -- bruce



    cmay wrote:
    > 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.
    >
    bruce barker, Dec 11, 2006
    #2
    1. Advertising

  3. cmay

    cmay Guest

    bruce barker wrote:
    > 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.
    cmay, Dec 12, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Quentin
    Replies:
    1
    Views:
    475
    Quentin
    Jul 29, 2003
  2. Jason Shohet
    Replies:
    1
    Views:
    474
    Kevin Spencer
    Nov 10, 2003
  3. Holger (David) Wagner
    Replies:
    2
    Views:
    1,930
    Holger (David) Wagner
    Jul 3, 2004
  4. Michael
    Replies:
    0
    Views:
    214
    Michael
    Dec 29, 2003
  5. Joe
    Replies:
    1
    Views:
    364
    Jeffrey Tan[MSFT]
    Feb 16, 2004
Loading...

Share This Page