Scratch this!
I have googled my ass off looking for this. 1. Lets say that i have a webform that has a few textboxes, with ID's textbox1, textbox2, textbox3. They all have viewstate enabled. I put some values in these textboxes and push a empty postback button and all the values are still there after postback. How can i access them in the viewstate after postback ? I would think that they were saved under the ID name of the textboxes but i dont get it to work like so. String s = ViewState["textbox1"].ToString(); I'm trying to get this to work because I want to be able to save the viewstate into the session so i can retrieve the info after i visit another webform. 2. Isn't it right that i can only use the viewstate on the same page that it was made on ? I could not use the viewstate on default.aspx in editor.aspx ? 3. And one more thing, isnt it right that the viewstate saves how a treeview nodes are expended ? I would like save the state on the treeview between two webforms that use the same masterpage.
EDIT:
Ok, this wasn't clear enough, thats a given. Basicly i'm trying to understand the viewstate and what i can do with it. I dont usually use viewstate to store values.
What i'm trying to do, or figure out if its possible with viewstate.
I have a masterpage and on the masterpage is a treeview. I have two pages that i use with the masterpage, Default.aspx and editor.aspx. I do my navigations and everything in the Default.aspx. When i have expanded the nodes in the treeview and selected one of the treenode, the navigateurl on that treenode send me to editor.aspx?navID=3. The editor.aspx uses the same masterpage and i want that page to show the SAME state on the treeview as the Default.aspx did 开发者_Go百科before i clicked on the node.
Take a look at this article to learn more about viewstate. I found it helpful
Truly understanding viewstate
The reason your code does not work is because ASP.NET uses a different name (I think it prefixes the control name with the form name and the master page name , if there is one). But even if you could pull it using that method, you shouldn't. You should manually add a property yourself to the viewstate. So if your trying to preserve the text in a text box, use the following code:
ViewState["TextBoxText"] = textbox1.ToString();
And to retreive this later, use:
String s = (String)ViewState["TextBoxText"];
To answer your questions:
- You are right. The viewstates are sacred to each individual page and cannot be accessed
- Treeview will automatically save the expanded nodes. Just make sure you are doing your initialzation to the treeview inside a if (!Page.IsPostBack) block.
The Viewstate collection in System.Web.UI.Control only allows you to access the viewstate bag for that control, not child controls. So basically you can't do what you want to do through ViewState.
You can get the values that a control posted through the Request.Form parameters. For example, if you have a control call textbox1 you could get its posted value through
Request.Form["textbox1"]
Depending on the control you may have to do some processing on the value you get out of there. For a treeview you can get the posted value of its expanded state using
Request.Form[TreeView1.ClientID + "_ExpandState"]
The value is a string with either an e (expanded) or an n (not expanded) for each node. So if the value was "eennene", nodes 1 2 5 and 7 would be expanded while the others would not be
精彩评论