| Author: chandramohan 04 Aug 2008 | Member Level: Gold | Rating: Points: 6 |
ViewState Next on our list of client side state management methods is Viewstate. This is an ASP.Net tool that allows you to maintain the state of your controls like textbox and listbox across page postbacks.
Viewstate has advantages the other 3 methods don't have. One of the most important is the ability of viewstate to support structured data. This means that control values are maintainable across page postbacks.
Using viewstate can be easy for nonpostback controls.
//use a keyvalue pair to save an object to viewstate. ViewState["sName"] = strName;
//Then to retrieve viewstate you have to convert to the object type //by unboxing the object using an explicit conversion. string sRetrieve; sRetrieve = (string) ViewState["sName"];
Disadvantages of viewstate
The more controls you have on the form the larger the size of viewstate and the larger the size of the HTML you send back and forth to the server. Only works when pages postback to themselves. You can't persist data to other pages. Even though the viewstate data is encrypted, it would be easy to hack the encrypted data. So you still don't want to save connection strings, passwords or credit card information in viewstate. The really cool thing about viewstate is it's ability to save structured data. Makes it very valuable to pass structured data back to itself on a page instead of going back to the database and re-retrieving the info or recreating the information each time.
Since viewstate is saved as HTML, ASP.Net gives you the ability to disable viewstate for individual controls, for entire pages, for an entire application and even for an entire machine. Very powerful.
For an individual control, just change the EnableViewState property to false to disable the control's viewstate. When a page doesn't postback to itself, meaning it is always sent to a new page, you can disable the page viewstate by addding a page directive.
<%@ Page EnableViewState="false" %>
At the application level you turn off view state in the web.config file. By disabling viewstate here, you disable the ability of any page to postback to itself and remember it's control's values.
<pages enableViewState="false" >
So, to summarize, there are 4 types of client side state management techniques. You can use querystrings, hidden fields, cookies and viewstate. They all have their advantages and disadvantages. You have to weigh the need to save the data before you can choose the proper technique. If you want to save structured data you have to choose viewstate. You want to persist data until the next time the user comes to your site? Then your choice is cookies. You want to hide information on a form and then send it to another site, then use hidden text boxes. Send information to another page, use the querystring.
But, remember the limitations of all of them. They are all client side, and they all have limited ability to secure data from the prying eyes of others. To increase security use Session state which is a server side state management technique.
|
| Author: vipul 04 Aug 2008 | Member Level: Diamond | Rating: Points: 2 |
hi, for that you used this way string str = ViewState["Name"].ToString()
vipul, http://dongavipul.blogspot.com
|
| Author: chandramohan 04 Aug 2008 | Member Level: Gold | Rating: Points: 6 |
Viewstate object is used to persist data of variables across postbacks. It even existed in classic ASP. In ASP.NET, a variable's value is assigned to a a viewstate object and then this is passed as a hidden variable and then may be retrieved by a page after a postback. See the example below...
//Save the value in ViewState object before the PostBack ViewState("SomeVar") = txtFirstName.text;
//Retrieve the value from ViewState object after the PostBack String strFirstName = ViewState("SomeVar").ToString();
Note that Viewstate object's value is accessible only at page level. This means that if a viewstate is created at page1.aspx, then it may be used only within page1.aspx after the postback, and cannot be used by any other page. To know how to pass values from one page to another
|
| Author: chandramohan 04 Aug 2008 | Member Level: Gold | Rating: Points: 6 |
View State As I mentioned briefly in the previous section, ASP.NET controls automatically retain their data when a page is sent to the server by a user clicking a submit button. Microsoft calls this persistence of data view state. In the past, developers would have to hack a way to remember the item selected in a drop-down menu or keep the contents of a text box, typically using a hidden form field. This is no longer the case; ASP.NET pages, once submitted to the server for processing, automatically retain all information contained within text boxes, items selected within drop-down menus, radio buttons, and check boxes. Even better, they keep dynamically generated tags, controls, and text. Consider the following ASP page, called sample.asp:
<html> <head> <title>Sample Page using VBScript</title> </head> <body> <form method="post" action="sample.asp"> <input type="text" name="txtName"/> <input type="Submit" name="btnSubmit" text="Click Me"/> <% If Request.Form("txtName") <> "" Then Response.Write(Request.Form("txtName")) End If %>
</form> </body> </html> If you save this example in the WebDocs subdirectory of wwwroot that you created in Chapter 1, Introduction to .NET and ASP.NET, you can open it in your browser by typing http://localhost/WebDocs/sample.asp, to see that view state is not automatically preserved. When the user submits the form, the information that was previously typed into the text box is cleared, although it is still available in Request.Form("txtName"). The equivalent page in ASP.NET, ViewState.aspx, demonstrates data persistence using view state:
Example 2.1. ViewState.aspx
<html> <head> <title>Sample Page using VB.NET</title> <script runat="server" language="VB"> Sub Click(s As Object, e As EventArgs) lblMessage.Text = txtName.Text End Sub </script> </head>
<body> <form runat="server"> <asp:TextBox id="txtName" runat="server" /> <asp:Button id="btnSubmit" Text="Click Me" OnClick="Click" runat="server" /> <asp:Label id="lblMessage" runat="server" /> </form> </body> </html> Example 2.2. ViewState.aspx
<html> <head> <title>Sample Page using C#</title> <script runat="server" language="C#"> void Click(Object s, EventArgs e) { lblMessage.Text = txtName.Text; } </script> </head>
<body> <form runat="server"> <asp:TextBox id="txtName" runat="server" /> <asp:Button id="btnSubmit" Text="Click Me" OnClick="Click" runat="server" /> <asp:Label id="lblMessage" runat="server" /> </form> </body> </html> In this case, the code uses ASP.NET controls with the runat="server" attribute. As you can see in Figure 2.2, the text from the box appears on the page when the button is clicked, but also notice that the data remains in the text box! The data in this example is preserved because of view state
|