views:

95

answers:

3

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 before i clicked on the node.

+2  A: 

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:

  1. You are right. The viewstates are sacred to each individual page and cannot be accessed
  2. Treeview will automatically save the expanded nodes. Just make sure you are doing your initialzation to the treeview inside a if (!Page.IsPostBack) block.
icemanind
More on question 2. Would i save the treeview nodes state in the viewstate and how would i get them back ?
eski
@eski I think you should post a more specific question, perhaps even deleting this one entirely (or atleast editing it heavily) to really address your overall scenario, i think you have some larger misconceptions about working with webforms and are going about solving your actual problem in a wrong way.
Chris Marisic
@eski I am pretty sure you don't need to do anything. The treeview control should automatically save the node states without you having to do it.
icemanind
I have updated the post, i wasnt clear enough before.
eski
+3  A: 

Take a look at this article to learn more about viewstate. I found it helpful

Truly understanding viewstate

desigeek
+1 That is an awesome article for learning what ViewState really does.
Dustin Hodges
+1  A: 

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

Dustin Hodges
I could use this, i would save this in the session and open again on the other webform.How would i inject a string to the treeview so it would expand the selected string. Basicly go the other way.. ?
eski
you'd have to do some processing on the string in code behind. Probably page load. Dig that string that contains the expanded nodes out of session on initial load and set the state of the nodes to either expanded or collapsed.
Dustin Hodges
Ok, it would be nice if i could just put the string in the treeview like String nodeState = "nncceencnecne";treeview.expandstate = nodestate;;)But i'm guessing i have to go through the nodes with foreach loop and inject each and everyone of the nodes.
eski