Adding controls programmatically.

Discussion in 'ASP .Net' started by Tim Marsden, May 19, 2004.

  1. Tim Marsden

    Tim Marsden Guest


    This is what I am doing, please comment if this is the correct way.

    I need to add controls to a form dynamically.
    Within the Page_Load event (is not Postback) I run a routine to create the
    controls, and populate the dropdowns etc,
    I use addhandler to define delegates to capture events raised by the
    I store these controls in a hashtable.
    I store the hashtables in the session store. (??)
    Then also within the Page_Load event (run each time), I repopulate the hash
    tables from the session store. I then run a routine to add the controls to
    and existing table on the form. (this bit is run every the page is posted as
    the controls disappear from the table)

    All seems OK so far.

    When the form is run, the events fire OK.

    However, if I place code within the event to amend properties of design time
    controls on the form, the changes are not reflected upon the form when it is
    next displayed. Run the same code in a design time control event and all is

    It seems I can amend properties of other dynamic controls but not design
    time controls.

    Please comment on my methods, and offer advise.


    I have placed a data set on the form, and populate this once in the page
    load. If I reference the dataset in the dynamic control events it is
    populated, if I reference it in the design time control events it is empty.

    There seems to be 2 environments, one for design time controls and one for
    dynamic controls. I am confused!!


    I am new to ASP.NET please see my other posts on the subject.
    Tim Marsden, May 19, 2004
    1. Advertisements

  2. Hi Tim,

    As for the dynamic created web server controls, here are some of my

    Yes, you're right the dynamically created control should be added into the
    page's control hierarchy everytime the page is loaded , not just once ,
    also if we add event handler for it, the handler also need to be registered
    everytime before the page's event processing occur so that we can make sure
    the event handler will be called.

    As for your situation, you do some changes on the designtime added controls
    in the dynamic control's eventhandler, I think the problem is that you
    haven't added the event handler for the dynamic control everytime,(no
    matter you first time created or retrieve from Session). Thus, you found
    that the event will fire only the first time:)
    In fact, the eventhandler info won't be stored in viewstate and even those
    design-time control, their event handler also need to be wiredup everytime
    the page loaded( you can find them in the InitializeComponent() method
    which is called in page's OnInit event).

    So , please try adding the eventhandler everytime the page is load and
    retest to see whether this helps. Thanks.


    Steven Cheng
    Microsoft Online Support

    Get Secure!
    (This posting is provided "AS IS", with no warranties, and confers no

    Get Preview at ASP.NET whidbey
    Steven Cheng[MSFT], May 20, 2004
    1. Advertisements

  3. Hi Tim,

    Have you had a chance to check out the suggestions in my last reply or have
    you got any further ideas on this issue? If you have anything unclear or if
    there're anything else we can help, please feel free to post here. Thanks.


    Steven Cheng
    Microsoft Online Support

    Get Secure!
    (This posting is provided "AS IS", with no warranties, and confers no

    Get Preview at ASP.NET whidbey
    Steven Cheng[MSFT], May 25, 2004
    1. Advertisements

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. =?Utf-8?B?RCBTaGVsZG9u?=

    Adding validation controls programmatically

    =?Utf-8?B?RCBTaGVsZG9u?=, May 11, 2004, in forum: ASP .Net
    May 11, 2004
  2. Chakravarti Mukesh

    Adding Controls Programmatically

    Chakravarti Mukesh, Jan 10, 2006, in forum: ASP .Net
    brijesh patel
    Feb 14, 2013
  3. Replies:
  4. Rob Meade
    Alexey Smirnov
    May 15, 2007
  5. Replies: