1 – 11 of 11
Blogger Sky Strider said...

This works to maintain the same controls on the page over the postbacks, however the whole point of viewstate is to maintain the user's input within the controls that were dynamically added. This is not being accomplished. How can the application retrieve the values the user places within the dynamically added web controls?

21 April, 2008 13:40

Blogger Yakko Warner said...

"This is not being accomplished."

Yes it is. That's the whole point of loading the controls before calling base.LoadViewState(). ASP.Net's own ViewState management process takes care of loading/saving the state of the dynamic controls, just like any other static control. You just have to set the conditions just right for the standard ViewState manager to work with the dynamic controls — ASP.Net does the rest, automatically.

"How can the application retrieve the values the user places within the dynamically added web controls?"

Easy. Once the steps are followed as I've outlined, then the controls get populated after LoadViewState, just like you'd expect.

It does work. I've done it just this way in both .Net 1.1 and 2.0

21 April, 2008 19:18

Blogger Mike said...

Yakko Warner is my new hero! I have been struggling with this off and on for a few years. I usually ended up hardcoding the number of controls and hiding those not currently active but that was a waste.


Worked perfectly in .Net 3.5.

30 May, 2008 17:42

Blogger Andy said...

Thank you! Adding the control to the page then setting the properties was the fix. I tried just about everything but that. I was about to go a new direction. You da man.

24 July, 2008 12:17

Blogger Eric said...

Yakko,

This was just what I needed to get over the hump of a project I'm working on... It's a novel solution and worked great! Thanks for this!

03 September, 2008 06:52

Blogger Rafael Ávila said...

Yakko, you are a genius!!! This solution works perfect well.

Thanks a lot!!!

11 November, 2008 07:09

Blogger Unknown said...

Hello, Yakko!

Thanks a lot for this example - the first thing that worked for me after a big struggle with dynamic controls!

However, i had to save and load not only the ids but also the values of my dynamic controls, in order for my application to work.

So i have 2 questions:

1. Can you show the code that is in "DocTypeSelectionControl.ascx"? I suppose that my error comes from my function for dynamic creation of controls and setting their values. (I followed the rule: When you create your controls, make sure you add them to the page FIRST, and THEN set any properties on them.)

2. Also can you also show the contents of "documents"?

Sorry if the questions are stuped, but i am a real newbe in .NET and C#!

Thanks in advance!

04 December, 2009 00:17

Blogger Yakko Warner said...

Unfortunately, I don't have access to the code anymore. I was laid off from that job long ago. In fact, I haven't been working in ASP.Net in a long while. But let's see what I can remember.

The idea, in this example, is that the code in OnLoad isn't the important part here. I only included it to show that the page is initially populated (note the !IsPostBack) with some controls, and how that might be done. It's actually a pretty bad example, because I don't like using Session when I can avoid it. But it was simpler than including a whole lot of code that showed the postback handlers for buttons that added new controls and stuff.

As I recall, the DocTypeSelectionControl.ascx was a very simple control. I think it just had an asp:TextBox that contained a document's filename, and an asp:DropDownList that had a list of document types (like "Tax Document", "Pay Stub", etc.).

The Document object (the collection of which is the "documents" variable, retrieved out of Session on the page's first load in this example) just had those two elements as properties (.Filename and .DocType), and the DocTypeSelectionControl.InitializeControl(Document) method just sets the value of the textbox and dropdown list to the .Filename and .DocType properties. There may have been an additional ID whose value was set to an asp:HiddenField control, I dunno.

The .ascx doesn't do anything special in its Initialize or Load events. You don't want to put any initializing code in its Load event, because that will undo anything the ViewState manager happens to restore. You can't even put it in there wrapped in a !IsPostBack event, because when you're adding a new control, you're doing it on postback, so a new control would never get initialized.

You shouldn't have to be storing the values of the controls in ViewState manually.

I whipped up an example project that should explain it a whole lot better. I used Visual Studio 2005 and .Net 2.0. You can download it here. The default page loads one instance of a dynamic control on its first load, and you can press a button to add as many additional controls as you wish. Each time you add a new control, there is a postback event where all existing controls' states are saved and loaded from ViewState. You can see that the controls themselves do nothing to keep their own values.

04 December, 2009 10:55

Blogger lnx4tw said...

I really appreciate the post! I was tearing my hair out trying to get this to work. This is ingenious!

06 December, 2009 16:43

Blogger Unknown said...

THANK YOU SO MUCH, YAKKO !!!

As i supposed, my error was that i initialized the dynamic controls in LoadViewState(). Now that i tested your example, everything is working all right in my project too!

Your example helped me not only to do my task, but also to realize the sequence of events in a web application and how to work with a user control!

THANKS A LOT!

11 December, 2009 02:07

Blogger positron said...

I Love you baby !!! its work lyk charm... you saved my time God!

23 October, 2010 08:34

Your civil comments are appreciated. Thanks!
You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.
Please prove you're not a robot