Asp.net dynamically adds custom save values โ€‹โ€‹after postback

Here is my problem. I have a usercontrol that I want to allow users to add as many instances as necessary by clicking a button (every time a button is clicked, I want to add another instance of my user control in Panel). It works great for the first time, but every additional post deletes all added controls back. I have no problem tracking the number of user controls added by the user, but how can I keep them in the same state as before the postback? I read some posts about people using SaveViewState and LoadViewState, but I could not find any examples.

My biggest problem is to ensure that all text fields and drop-down lists from each user control are filled with the same text / selected value / data after each post back

Thanks in advance Ben

+7
c # user-controls dynamically-generated
source share
7 answers

As you programmatically add controls to your page, you need to recreate them on EVERY postback.

In addition, you need to update programmatically added controls on the PreInit or Init events on the page. This is necessary to properly manage events in view mode.

If you do not, the control will be disabled after feedback, and they will not process any event.

EDIT

Although it is recommended that you add dynamic controls to PreInit or Init , it is true (as Dustin Hodges says) that it can work if you add them to page_load . I would say that you should avoid this if you have no other choice.

You can get away with loading the controls in the Page_Load event handler and properly save the view state.
It all depends on whether you set any properties of dynamically loaded controls programmatically, and if so, when you do this relative to the Controls.Add (dynamicControl) line.
A thorough discussion of this issue is slightly beyond the scope of this article, but the reason it can work is because the Control Add () method recursively loads the parent view into its children, even if the loading view state has passed.

MSDN Source

+10
source share

If you track the number of controls added by the user, you need to recreate the controls added by the user earlier, preferably in Page_Init or Page_Load. Add something like this to this handler:

 for(int i=0; i<NumberOfControlsUserHasAdded; i++) { //todo: change this to the appropriate user control TextBox tb = new TextBox(); tb.ID = "tb" + i.ToString(); //todo: add to appropriate control collection this.Controls.Add(tb); } 

If you do this like this, the state of the controls should be maintained, because when you add the tb control to the collection of controls, it plays catch-up events and should automatically restore its view.

You do not need to track your state in the session, as in most cases it will be stored in the view for you

+2
source share

I had a nightmare of time trying to remove this from an old project. In the interim, I found that I know a lot less about web development than I thought (reading this site is a great way to put up with everyday, if not hourly). In this project, the page.IsPostBack page was completely useless to me because I dynamically created controls.

Saying the best I can offer is to consider using the Session variable. If you have a class (or collection of a class) that represents the data that you grab from the page, then it might be easier to store the values โ€‹โ€‹in that class / collection in order to improve the readability of the code and then write it to the session.

+1
source share

Look at your PageLoad and add if(!this.IsPostBack) before the line where you are removing the panel.

This, of course, is only an assumption, but I saw many questions where the problem was related to this.

0
source share

I think what might happen when you click the button to add, Page_Load fires, which creates a new page. Then the button click method adds the control, and the page finishes rendering. When you click it a second time, Page_Load will create your page, and the button click method will create the control again and add it to the page. Unfortunately, the one you added earlier no longer exists.

What if you added controls to the repeater? The view state of a Repeater can be monitored by each control that you add so that it does not deflate on the back.

0
source share

This is an ugly solution if you have many users, but you can embed the user controls themselves in the session. I do this with the footer control because I don't want to hit db every time the page changes to recreate the footer.

These solutions can actually perform tasks on the server if you have many users and they use this feature a lot. At least I think it will be ...

But you can just refill your placeholder, which has usercontrols in it on page_load. I can give an example soon.

An example of what I am doing:

 if (Session["footer"] == null) { Session["footer"] = new Footer(LinksRules.BuildFooterLinks((int)WebSiteSections.Main));// where Footer is my control } footerPH.Controls.Add((Footer)Session["footer"]); 

As a singleton view ...

So, as I see it, you can do it at all that will cause the postback

Session ["DynamicControls"] = PlaceHolder.Controls;

and in the method of loading the page, you can:

 foreach(var control in (List<Controls>)Session["DynamicControls"]) { yourPlaceHolder.Controls.Add(control); } 

and if the session object is null, just add one as if they were never there.

I believe this will hang with the data inside the controls as you want.

0
source share

All Articles