Adding controls programmatically.

T

Tim Marsden

Hi,

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
controls.
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
OK.

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

Please comment on my methods, and offer advise.

Secondly.

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!!

Tim.

I am new to ASP.NET please see my other posts on the subject.
 
S

Steven Cheng[MSFT]

Hi Tim,

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

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.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
S

Steven Cheng[MSFT]

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.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top