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";
}

Bound grid
DataGrid1.DataSource = DV;
Datagrid1.databind ();
}

void Sortgrid (object sender, DataGridSortCommandEventArgs e) {

Datagrid1.currentpageindex = 0;
SortField = e.sortexpression;
Bindgrid ();
}

</script>

The following are the Testdata.xml code referenced in the two preceding snippets:

<?xml version= "1.0" standalone= "yes"?>
<NewDataSet>
<Table>
<pub_id>0736</pub_id>
<pub_name>new Moon books</pub_name>
<city>Boston</city>
<state>MA</state>
<country>USA</country>
</Table>
<Table>
<pub_id>0877</pub_id>
<pub_name>binnet & Hardley</pub_name>
<city>Washington</city>
<state>DC</state>
<country>USA</country>
</Table>
<Table>
<pub_id>1389</pub_id>
<pub_name>algodata infosystems</pub_name>
<city>Berkeley</city>
<state>CA</state>
<country>USA</country>
</Table>
<Table>
<pub_id>1622</pub_id>
<pub_name>five Lakes publishing</pub_name>
<city>Chicago</city>
<state>IL</state>
<country>USA</country>
</Table>
<Table>
<pub_id>1756</pub_id>
<pub_name>ramona publishers</pub_name>
<city>Dallas</city>
<state>TX</state>
<country>USA</country>
</Table>
<Table>
<pub_id>9901</pub_id>
<pub_name>GGG&G</pub_name>
<city>Muenchen</city>
<country>Germany</country>
</Table>
<Table>
<pub_id>9952</pub_id>
<pub_name>scootney books</pub_name>
<city>new york</city>
<state>NY</state>
<country>USA</country>
</Table>
<Table>
<pub_id>9999</pub_id>
<pub_name>lucerne publishing</pub_name>
<city>Paris</city>
<country>France</country>
</Table>
</NewDataSet>



Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.