Preliminary study on ASP.net ViewState (2) from MSDN
Source: Internet
Author: User
asp.net See the following example: To display a list of items on a Web page, each user needs a different sort of list. The list of items is static, so you can bind these pages to the same cached dataset, which is only a small part of the user-specific UI state. ViewState is ideal for storing this type of value. The code is as follows:
[Visual Basic]
<%@ Import namespace= "System.Data"%>
<HTML>
<HEAD>
<title> viewstate/title> for page UI state values
</HEAD>
<body>
<form runat= "Server" >
<H3>
Storing non-control state in ViewState
</H3>
<P>
This example stores the current sort order for a column of static data in ViewState. <br>
Click the link in the column header to sort the data by that field. <br>
Clicking the link again will be sorted in reverse order.
<br><br><br>
<asp:datagrid id= "DATAGRID1" runat= "Server"
Onsortcommand= "Sortgrid" borderstyle= "None" borderwidth= "1px"
Bordercolor= "#CCCCCC" backcolor= "White" cellpadding= "5" allowsorting= "True" >
Backcolor= "#006699" >
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<script runat= "Server" >
' Tracking SortField properties in ViewState
Property SortField () as String
Get
Dim o as Object = ViewState ("SortField")
If O is nothing Then
Return String.Empty
End If
return CStr (o)
End Get
Set (Value as String)
If Value = SortField Then
' Same as current sort file, toggle sort Direction
SortAscending = Not sortascending
End If
ViewState ("SortField") = Value
End Set
End Property
' Tracking sortascending properties in ViewState
Property sortascending () as Boolean
Get
Dim o as Object = ViewState ("sortascending")
If O is nothing Then
Return True
End If
return CBool (o)
End Get
Set (Value as Boolean)
ViewState ("sortascending") = Value
End Set
End Property
Private Sub Page_Load (sender as Object, e as EventArgs) Handles MyBase.Load
If not Page.IsPostBack Then
Bindgrid ()
End If
End Sub
Sub Bindgrid ()
' Get Data
Dim DS as New DataSet ()
Ds. READXML (Server.MapPath ("Testdata.xml"))
Dim DV as New DataView (ds. Tables (0))
' Apply sort filters and Directions
Dv. Sort = SortField
If not sortascending Then
Dv. Sort + = "DESC"
End If
' Bind grid
DataGrid1.DataSource = DV
Datagrid1.databind ()
End Sub
Private Sub Sortgrid (sender as Object, E as DataGridSortCommandEventArgs)
Datagrid1.currentpageindex = 0
SortField = E.sortexpression
Bindgrid ()
End Sub
</script>
[C #]
<%@ Page language= "C #"%>
<%@ Import namespace= "System.Data"%>
<HTML>
<HEAD>
<title> viewstate</title> for page UI state values
</HEAD>
<body>
<form runat= "Server" >
<H3>
Storing non-control state in ViewState
</H3>
<P>
This example stores the current sort order for a column of static data in ViewState. <br>
Click the link in the column header to sort the data by that field. <br>
Clicking the link again will be sorted in reverse order.
<br><br><br>
<asp:datagrid id= "DATAGRID1" runat= "Server" onsortcommand= "Sortgrid"
Borderstyle= "None" borderwidth= "1px" bordercolor= "#CCCCCC"
Backcolor= "White" cellpadding= "5" allowsorting= "True" >
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<script runat= "Server" >
Tracking SortField Properties in ViewState
String SortField {
get {
Object o = viewstate["SortField"];
if (o = = null) {
return String.Empty;
}
Return (string) O;
}
set {
if (value = = SortField) {
Same as the current sort file, toggle the sort direction
SortAscending =! sortascending;
}
viewstate["SortField"] = value;
}
}
Tracking SortAscending Properties in ViewState
BOOL SortAscending {
get {
Object o = viewstate["sortascending"];
if (o = = null) {
return true;
}
return (BOOL) O;
}
set {
viewstate["sortascending"] = value;
}
}
void Page_Load (object sender, EventArgs e) {
if (! Page.IsPostBack) {
Bindgrid ();
}
}
void Bindgrid () {
Get Data
DataSet ds = new DataSet ();
Ds. READXML (Server.MapPath ("Testdata.xml"));
DataView dv = new DataView (ds. Tables[0]);
Apply sorting filters and orientations
Dv. Sort = SortField;
if (! sortascending) {
Dv. Sort = = "DESC";
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service