persiting data in asp.net

  • Thread starter Vishal Patel via .NET 247
  • Start date
V

Vishal Patel via .NET 247

Hi

I am finding problem with dynamically created controls in asp.net
When I Create a text box at runtime with a cycle when next time the cycle executes again it simply remove the entered data from text box this is not happening if i create a text box dynamically at
runtime.I need to create text box on click event of button.
So please suggest me how can I persist data of them on each successive load.
 
B

Brock Allen

If you're dynamically creating controls on the form you need to also dynamically
create them on the PostBack to your Form. The two places to do this are either
in Page_Init or in CreateChildControls. Try Page_Init first as that's the
easitest to get going. Now the hard part about this is "remembering" across
postbacks what controls you dynamically added, so it's up to you to track
this state somewhere.
 
H

HoustonFreeways

As the other poster mentioned, you need to rebuild the dynamic controls on
the postback. I've found the best way to do this is to save the information
needed to generate the controls in the viewstate and get the viewstate data
at the Page_init, then rebuild the page exactly as it was. For a datagrid
you would save the datasource datatable in the viewstate (by serializing it)
and then recover it at the page_init and rebind the datagrid.

One you rebuild exactly the same control tree, ASP.Net can get the data for
data entry controls such as textboxes.
 
B

Brock Allen

As the other poster mentioned, you need to rebuild the dynamic
controls on the postback. I've found the best way to do this is to
save the information needed to generate the controls in the viewstate
and get the viewstate data at the Page_init, then rebuild the page
exactly as it was. For a datagrid you would save the datasource
datatable in the viewstate (by serializing it) and then recover it at
the page_init and rebind the datagrid.

You've been successful in this approach? ViewState shouldn't be available
in Page_Init as ASP.NET has not yet loaded ViewState back from the persistence
medium (the __VIEWSTATE hidden field) in Page_Init. So I'm dubious of this
approach.

Now if you've stored your own data in your own hidden field then of course
this approach would work. But there's a chicken and the egg problem with
relying upon viewstate, because if ViewState is available then it means it's
too late to recreate all the controls because the won't have their ViewState
loaded.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top