[Visual Basic]
<%@ Import Namespace=
<HTML>
<HEAD>
<title>用於頁面 UI 狀態值的 ViewState/title>
</HEAD>
<body>
<form runat=
<H
在 ViewState 中存儲非控件狀態
</H
<P>
此示例將一列靜態數據的當前排序順序存儲在 ViewState 中
單擊列標題中的鏈接
再次單擊該鏈接
<br><br><br>
<asp:datagrid id=
OnSortCommand=
BorderColor=
<HeaderStyle Font
BackColor=
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<script runat=
Property SortField() As String
Get
Dim o As Object = ViewState(
If o Is Nothing Then
Return String
End If
Return CStr(o)
End Get
Set(Value As String)
If Value = SortField Then
SortAscending = Not SortAscending
End If
ViewState(
End Set
End Property
Property SortAscending() As Boolean
Get
Dim o As Object = ViewState(
If o Is Nothing Then
Return True
End If
Return CBool(o)
End Get
Set(Value As Boolean)
ViewState(
End Set
End Property
Private Sub Page_Load(sender As Object
If Not Page
BindGrid()
End If
End Sub
Sub BindGrid()
Dim ds As New DataSet()
ds
Dim dv As New DataView(ds
dv
If Not SortAscending Then
dv
End If
DataGrid
DataGrid
End Sub
Private Sub SortGrid(sender As Object
DataGrid
SortField = e
BindGrid()
End Sub
</script>
[C#]
<%@ Page Language=
<%@ Import Namespace=
<HTML>
<HEAD>
<title>用於頁面 UI 狀態值的 ViewState</title>
</HEAD>
<body>
<form runat=
<H
在 ViewState 中存儲非控件狀態
</H
<P>
此示例將一列靜態數據的當前排序順序存儲在 ViewState 中
單擊列標題中的鏈接
再次單擊該鏈接
<br><br><br>
<asp:datagrid id=
BorderStyle=
BackColor=
<HeaderStyle Font
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<script runat=
// 在 ViewState 中跟蹤 SortField 屬性
string SortField {
get {
object o = ViewState[
if (o == null) {
return String
}
return (string)o;
}
set {
if (value == SortField) {
// 與當前排序文件相同
SortAscending = !SortAscending;
}
ViewState[
}
}
// 在 ViewState 中跟蹤 SortAscending 屬性
bool SortAscending {
get {
object o = ViewState[
if (o == null) {
return true;
}
return (bool)o;
}
set {
ViewState[
}
}
void Page_Load(object sender
if (!Page
BindGrid();
}
}
void BindGrid() {
// 獲取數據
DataSet ds = new DataSet();
ds
DataView dv = new DataView(ds
// 應用排序過濾器和方向
dv
if (!SortAscending) {
dv
}
// 綁定網格
DataGrid
DataGrid
}
void SortGrid(object sender
DataGrid
SortField = e
BindGrid();
}
</script>
下面是上述兩個代碼段中引用的 testdata
<?xml version=
<NewDataSet>
<Table>
<pub_id>
<pub_name>New Moon Books</pub_name>
<city>Boston</city>
<state>MA</state>
<country>USA</country>
</Table>
<Table>
<pub_id>
<pub_name>Binnet & Hardley</pub_name>
<city>Washington</city>
<state>DC</state>
<country>USA</country>
</Table>
<Table>
<pub_id>
<pub_name>Algodata Infosystems</pub_name>
<city>Berkeley</city>
<state>CA</state>
<country>USA</country>
</Table>
<Table>
<pub_id>
<pub_name>Five Lakes Publishing</pub_name>
<city>Chicago</city>
<state>IL</state>
<country>USA</country>
</Table>
<Table>
<pub_id>
<pub_name>Ramona Publishers</pub_name>
<city>Dallas</city>
<state>TX</state>
<country>USA</country>
</Table>
<Table>
<pub_id>
<pub_name>GGG&G</pub_name>
<city>Muenchen</city>
<country>Germany</country>
</Table>
<Table>
<pub_id>
<pub_name>Scootney Books</pub_name>
<city>New York</city>
<state>NY</state>
<country>USA</country>
</Table>
<Table>
<pub_id>
<pub_name>Lucerne Publishing</pub_name>
<city>Paris</city>
<country>France</country>
</Table>
</NewDataSet>
From:http://tw.wingwit.com/Article/program/net/201311/12702.html