Disable viewstate loading for child controls
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开发者_StackOverflow中文版 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.
You can try creating the new tree after the viewstate has been loaded, e.g. in Page_Load.
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.
I have answered it here:
How to load a page with its default values after a postback
精彩评论