HOW TO MAINTAIN DYNAMICALLY CREATED CONTROLS AFTER POSTBACK C#
-
Hi, i'm creating a TextBox dynamically with the firing of a Button but every time the page posts back the previous TextBox disappears. Is there a way to retain that control and add the new ones below it? Your help is greatly appreciated! Fivos
-
Hi, i'm creating a TextBox dynamically with the firing of a Button but every time the page posts back the previous TextBox disappears. Is there a way to retain that control and add the new ones below it? Your help is greatly appreciated! Fivos
hi phivos, the concept here is that the server recognizes the controls that are present in the design view of the page... and produces the HTML code for those. the control you created is dynamic and will be maintained only if their is some logic to add that control every time. we can a collection object declared and add a key value pair for that control i.e. "textbox1" , "value in textbox entered by user" and add it to that collection and keep that collection object in viewstate. everytime in page load, check for number of key-value pairs in that object and run a loop to add that much controls with id as key and TEXT as value for that key... simple solution ;) cheers..!
Ashish Sehajpal
-
hi phivos, the concept here is that the server recognizes the controls that are present in the design view of the page... and produces the HTML code for those. the control you created is dynamic and will be maintained only if their is some logic to add that control every time. we can a collection object declared and add a key value pair for that control i.e. "textbox1" , "value in textbox entered by user" and add it to that collection and keep that collection object in viewstate. everytime in page load, check for number of key-value pairs in that object and run a loop to add that much controls with id as key and TEXT as value for that key... simple solution ;) cheers..!
Ashish Sehajpal
-
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx[^] try here, a simple anf useful control..
if you are adding control(s) dynamically on the page, you will loos all the added controls on postback. To avoid this...you need to understand the fundamental of aspx. on the postback page is loosing all the dynamically added controls. Basically all the controls are available on the page, but page can not retrive them from viewstate. You need to make rememebr the ids of dynamica added controls to the page. ie. if you are added 2 textboxes on the page and giving ids like txt.ID="txt" +1 and for second txt.ID="txt" +2. so on the page load write the code to add the two textboxes and assign the same ids, so, page autometically will retain the text entered in the textbox on the postback. Enjoy