views:

277

answers:

3

I have a lot of web controls created dynamically in an ASP.NET page. In certain postback scenarios (when I click a LinkButton), I want to skip the loading of the old tree of web controls and immediately generate the new tree. Unfortunately, when I generate the new tree, the viewstate of the old tree is loaded into it.

I want to be able to disable the viewstate loading process for this specific scenario, but after the new tree is loaded, the viewstate should work normally.

I've already solved part of the problem, by overriding the LoadViewState method of the web controls, but, unfortunately, this disables the viewstate specific for the control, not for his children too (textboxes, buttons etc.).

Is it possible?

Thank you.

A: 

You can try creating the new tree after the viewstate has been loaded, e.g. in Page_Load.

Per Erik Stendahl
Yes, I actually load the new controls in the LinkButton.Clicked event which comes after Page_Load, but the way the ASP.NET works, the viewstate loading will be deffered until you actually load new controls into a rooted control (one that has parents all the way up to the Page).
Florin Sabau
Ah, dynamically added controls. That's always such a mess.. :) I don't know how your code is structured but perhaps you can hook into your dynamic controls load event and create the new tree in that handler?
Per Erik Stendahl
Not really sure what you meant by "hook into your dynamic controls load event and create the new tree in that handler".
Florin Sabau
A: 

You can do this programatically by clearing the contents of the tree before binding the new data to it. It should be somehting along the lines of

tree.nodes.clear()

and then you just go on as if the tree is empty and add you data by either bounding it or by adding the nodes programatically with

tree.Nodes.Add(New TreeNode("name of node"))

now for regular controls which I gather you are using you will run into a whole host of problems if you don't use viewstate. You can remove them from the control tree in a similar fashion using controlname.controls.clear. Now is you problem primarily the appending of these contorls or is it that you don't want to carry the burden of viewstate? I suggest that if it's the latter that you clear your controls programatically and store viewstate server side somewhere so that it can be loaded from memory instead of using your bandwidth.

As for dynamical loading of controls you will need to ensure tha tyour new controls carry different ID's for them. What I've done in one of my apps is actually keep the old controls, make them invisible and clear the viewstate then on the next call I actually remove them from the tree.

Middletone
I'm not really using a tree. When a said "tree" I was referring to the tree of controls that I add dynamically. The problem that I'm trying to avoid is generating a very time consuming list of controls on LinkButton.Click, only to make asp.net happy and load viewstate and then forget about them and recreate with the new set of controls. I'm trying to avoid the first step. Normally I wouldn't go to this length to modify the normal behavior of asp.net creation of dynamic controls, but as I said, this operation is in this case quite time consuming and I want to avoid it altogether.
Florin Sabau
A: 

I have answered it here:

How to load a page with its default values after a postback

Shrage Smilowitz