Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Maintaining State
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00761789
Message ID:
00761987
Vues:
30
Hi Bonnie,

ViewState is programmatic when dynamically adding controls. You need to make sure you istantiate the control in the Page_Load (sometimes it requires istantiation in the Page_Init). The control needs to be created before the ViewState information is processed on the server. There are some real known issues with ViewState not getting the correct control id that was dynamically added.

>This problem is driving me crazy, I hope someone has some practical ideas.
>
>I have a UserControl that I am adding programmatically to my WebForm when a User clicks a choice.
>
>this.oMyControl = (MyControl)LoadControl("MyControl.ascx");
>this.oMyControl.oData = this.oData;
>this.MyPanel.Controls.Add(this.oMyControl);
>
>this.DataBind();
>
>OK, this works fine. I have persisted my DataSet in the ViewState (because it's a small DataSet, I don't have a problem with doing this). In the UserControl I have several TextBox controls, that are filled from the DataSet. And this works fine as well.
>
>The problem I'm having is trying to save changes the user makes in the TextBoxes on the UserControl. This is because I don't just have *one* UserControl ... this form can have several UserControls (only one instantiates on the page at a time) and I'd like to be able to preserve any changes in any of the TextBox controls, even when the user clicks on another choice and a different UserControl instantiates.
>
>What happens currently is that when another choice is made, and the page posts back to the server, the this.MyPanel.Controls.Count is 0 ... IOW, this.oMyControl is no longer contained in this.MyPanel.Controls. Why not? I'm guessing it would have something to do with ViewState, but I'm not exactly sure how to add this.oMyControl to the ViewState, or even if I'm approaching this from the right angle. oMyControl has EnableViewState true and so does MyPanel. I seem to remember reading somewhere about problems with keeping programmatically added UserControls in the ViewState, but I can't find any practical ways of making sure it gets added.
>
>I'm sure I'm not explaining this very well, but hopefully one of you guys will get the gist of what I'm saying and what I'm asking about.
>
>Any suggestions from someone who's been there?
>
>TIA,
>~~Bonnie
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform