Best practice - dynamic controls vs page life cycle vs viewstate

Discussion in 'ASP .Net' started by HP, Mar 25, 2007.

  1. HP

    HP Guest

    Hi there

    The problem of dynamically created controls vs viewstate is widely
    known one. To access values of controls they have to be recreated on
    Page_Load.

    Unfortunately it causes many problems in the following (rather common,
    I guess) scenario:

    The controls are created dynamically. Their number and contents
    depends on some DB values. User can add/delete data which should cause
    changing the number of controls.

    The problem:

    Controls have to be drawn during page_load. Events have to be handled
    during handlers cycle which occurs AFTER page_load. So if user changes
    the state of database, the controls will be drawn based on data
    fetched BEFORE the new changes are applied.


    I'm trying to overcome this by checking on pageload which button was
    fired (by iterating through Request.Form) but it's a rather dirty
    solution.

    thanks for your ideas
    HP
    HP, Mar 25, 2007
    #1
    1. Advertising

  2. I have done numerous dynamic control pages and have not had the problems you
    have. I currently do not have an example, so I will have to dig something
    up, but I do not remember as much pain.

    I would stay away from using Page_Load to check things, as much as possible.
    There may be an exception to this rule that I have missed, but Page_Load for
    anything other than loading when not IsPostBack is generally a bad idea.

    The ViewState fires off in between the Init routine(s) and Load, so you can
    pull a lot back prior to page load. I do not think I did anything super
    special with dynamic controls, but, if so, I would paint them in at
    Page_Load initially and then factor them in as ViewState is grabbed.

    I will have to see if I have time to resurrect some examples I have done
    (older production samples from previous assignments) or find an URL for you.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    *********************************************
    Think outside the box!
    *********************************************
    "HP" <> wrote in message
    news:...
    > Hi there
    >
    > The problem of dynamically created controls vs viewstate is widely
    > known one. To access values of controls they have to be recreated on
    > Page_Load.
    >
    > Unfortunately it causes many problems in the following (rather common,
    > I guess) scenario:
    >
    > The controls are created dynamically. Their number and contents
    > depends on some DB values. User can add/delete data which should cause
    > changing the number of controls.
    >
    > The problem:
    >
    > Controls have to be drawn during page_load. Events have to be handled
    > during handlers cycle which occurs AFTER page_load. So if user changes
    > the state of database, the controls will be drawn based on data
    > fetched BEFORE the new changes are applied.
    >
    >
    > I'm trying to overcome this by checking on pageload which button was
    > fired (by iterating through Request.Form) but it's a rather dirty
    > solution.
    >
    > thanks for your ideas
    > HP
    >
    Cowboy \(Gregory A. Beamer\), Mar 26, 2007
    #2
    1. Advertising

  3. HP

    bruce barker Guest

    dynamic control should be recreated in oninit so they can receive their
    postback data and fire events.

    if the page need to change controls, then in the page load, or click
    event delete the old controls (simple if in a place holder) and create
    new ones before prerender.

    -- bruce (sqlwork.com)

    HP wrote:
    > Hi there
    >
    > The problem of dynamically created controls vs viewstate is widely
    > known one. To access values of controls they have to be recreated on
    > Page_Load.
    >
    > Unfortunately it causes many problems in the following (rather common,
    > I guess) scenario:
    >
    > The controls are created dynamically. Their number and contents
    > depends on some DB values. User can add/delete data which should cause
    > changing the number of controls.
    >
    > The problem:
    >
    > Controls have to be drawn during page_load. Events have to be handled
    > during handlers cycle which occurs AFTER page_load. So if user changes
    > the state of database, the controls will be drawn based on data
    > fetched BEFORE the new changes are applied.
    >
    >
    > I'm trying to overcome this by checking on pageload which button was
    > fired (by iterating through Request.Form) but it's a rather dirty
    > solution.
    >
    > thanks for your ideas
    > HP
    >
    bruce barker, Mar 26, 2007
    #3
  4. HP

    HP Guest

    On Mar 26, 4:23 am, "Cowboy \(Gregory A. Beamer\)"
    <> wrote:
    > I have done numerous dynamic control pages and have not had the problems you
    > have. I currently do not have an example, so I will have to dig something
    > up, but I do not remember as much pain.




    > I would stay away from using Page_Load to check things, as much as possible.
    > There may be an exception to this rule that I have missed, but Page_Load for
    > anything other than loading when not IsPostBack is generally a bad idea.


    could you explain why is that? because it's possible that something
    was changed before the Page_Load in the code?

    thanks
    HP
    HP, Apr 1, 2007
    #4
    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. TB
    Replies:
    12
    Views:
    819
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Oct 27, 2005
  2. Fred
    Replies:
    5
    Views:
    6,233
    Karl Seguin [MVP]
    Jan 23, 2006
  3. lander
    Replies:
    5
    Views:
    2,521
    Mark Rae [MVP]
    Mar 3, 2008
  4. Gary Larimer

    Page life cycle and Page.IsValid property

    Gary Larimer, Jul 30, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    378
    Gary Larimer
    Aug 1, 2008
  5. Ryan Liu
    Replies:
    3
    Views:
    2,213
    Ryan Liu
    Mar 27, 2009
Loading...

Share This Page