Dynamically added controls and postback

M

Matthew Smith

I have a situation where I have to dynamically add controls at runtime.
I am not sure how to deal with postback and retaining values in
textboxes. I have seen examples where you have to include properties
with the same name in the C# codebehind class. But this defeats the use
of dynamic controls. Is there anyway to access and maintain the values
in the fields?
 
G

Guest

Matthew:

I'm currently working on a project that needs to do the same thing you're describing. I had a lot of trouble finding good information on the topic, but after a lot of googling and even more trial/error, what I did was as follows:

- Create an HTML table on my form, gave it an ID and set runat="server"

- Wrote some functions that handle the layout. Basically what I did was to have the functions create new controls, append the controls to new HtmlTableCells, append the cells to new HtmlTableRows and then append the rows to the table on my form.

- Place a call to the layout functions in my page's OnInit. This, from what I was able to gather on various posts I found is critical.

- On postback, I get a reference to my form's table, then use FindControl(ControlName) to grab each control and get it's value and process as needed.

I've had no problems with this approach. I'm sure there are better (easier?) ways to handle this, but this method seems to be working out fine for the time being. If this needs any clarification, respond and I'll see what I can do.

----- Matthew Smith wrote: -----

I have a situation where I have to dynamically add controls at runtime.
I am not sure how to deal with postback and retaining values in
textboxes. I have seen examples where you have to include properties
with the same name in the C# codebehind class. But this defeats the use
of dynamic controls. Is there anyway to access and maintain the values
in the fields?
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top