In ASP.net, ViewState because in the client's HTML occupies a lot of space, and as the page postback repeatedly passed in the network, has been for human scale disease. But in fact ViewState can be stored anywhere in the database, cache, and so on, avoiding the frequent sending of lengthy base64 strings to clients. This will not only significantly improve performance (significantly reduce the number of bytes transmitted by the network), and if the content is not easily decrypted and cracked. Therefore, this method is very useful.
The following is a simple example of using caching as a ViewState storage destination. As for the cache Key, the article gives only a simple wording, concrete can be given according to the circumstances of the strict scheme.
The code is roughly illustrated as follows:
<%@ Page language= "C # codebehind=" SaveViewStateToOther.aspx.cs "autoeventwireup=" false "inherits=" Linkedlist.saveviewstatetoother "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<html>
<head>
<title>SaveViewStateToOther</title>
<meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" >
<meta name= "Code_language" content= "C #" >
<meta name=vs_defaultclientscript content= "JavaScript" >
<meta name=vs_targetschema content= "http://schemas.microsoft.com/intellisense/ie5" >< br/> </head >
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "server" ><asp:datagrid id=datagrid1 "style= z-index:101"; left:104px; Position:absolute; top:72px "runat=" Server "bordercolor=" #3366CC "borderstyle=" None "borderwidth=" 1px "backcolor=" White "cellpadding=" 4 "Pagesize=" 6 "allowpaging=" True ">
<selecteditemstyle font-bold= "True" forecolor= "#CCFF99" backcolor= "#009999" >
</SelectedItemStyle>
<itemstyle forecolor= "#003399" backcolor= "white" >
</ItemStyle>
<headerstyle font-bold= "True" forecolor= "#CCCCFF" backcolor= "#003399" >
</HeaderStyle>
<footerstyle forecolor= "#003399" backcolor= "#99CCCC" >
</FooterStyle>
<pagerstyle horizontalalign= "left" forecolor= "#003399" backcolor= "#99CCCC" pagebuttoncount= "" mode= " NumericPages ">
</PagerStyle>
</asp:DataGrid>
</form>
</body>
</html>
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LinkedList
{
///<summary>
Summary description of the
///Saveviewstatetoother.
///</summary>
public class Saveviewstatetoother:page
{
protected DataGrid DataGrid1;
private void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
Bind ();
}
private void Bind ()
{
datatable table = new DataTable ();
table. Columns.Add ("id", typeof (int));
table. Columns.Add ("name", typeof (String));
for (int i = 0; i < 1000; i++)
{
DataRow row = table. NewRow ();
row["id" = i;
row ["name"] = "Student_" + i.tostring ();
table. Rows.Add (row);
}
DataGrid1.DataSource = table;
Datagrid1.databind ();
}