views:

280

answers:

4

Hi,

I am adding HTML input controls on the page dynamically via a "add text box" button but after post back eventually they are being washed away. Is there an easy and good practice which can help me keep the controls and their values after page post back.

Thanks.

Edit :

  • I am using Javascript to create dynamic controls on the page
  • I am not dealing with File Uploads, just creating custom field things.

I think I need to override SaveViewState and LoadViewState events to keep my controls in the ViewState.

A: 

since you will receive them on post back, could you not recreate them dynamically at server-side ?

Gaby
This is also what I was thinking but since it is pretty common technique I thought there maybe another and better way to do it.
Braveyard
The control collections for my company's pages are almost completely built in the code-behind, because we have to support user-definable layouts. I think Gaby's idea will work fine but you may run into some conflicts with the dynamically added controls and the viewstate. Whether or not you experience this problem will depend on how your page gets used.
Jeremy Bade
Then, the MVC framework would work for your company better than Web Forms do..
Braveyard
A: 

If the controls differ from postback to postbck depending on what the user enters then you can try storing the html ito a seperate class where you will have static properties like public static string WriteHTMLSubmit = ""

If not then hard code them in code behind, this being the bes practice... if(page.isPostBack){<write contrls>}

GxG
+2  A: 

Keep the page! Use ajax to post the data, and a flash uploader if you are dealing with file uploads. Then the page never refreshes, and there is a lot less code to re-do the dynamically created controls.

gahooa
I thought the same thing but the page is huge so it wouldn't be good practice using Ajax I think. They are too many data posted back to the server. And I am not dealing with Uploads actually, what I am doing is creating custom fields and send the values to the data along with the other values.
Braveyard
+1  A: 

See followings:
http://urenjoy.blogspot.com/2009/02/add-dynamic-buttons-and-handle-click.html
You need to override SaveViewState and LoadViewState methods to save after postback, See following sample
http://urenjoy.blogspot.com/2009/03/create-dynamic-dropdownlists-in-aspnet.html
and To retrieve value see following example:
http://urenjoy.blogspot.com/2009/02/retrieve-value-of-dynamic-controls-in.html

Brij
Thanks for the recommendations, I will check them out now. I hope I can find the answer there.
Braveyard