请看下面的示例
要在 Web 页上显示一个项目列表
而每个用户需要不同的列表排序
项目列表是静态的
因此可以将这些页面绑定到相同的缓存数据集
而排序顺序只是用户特定的 UI 状态的一小部分
ViewState 非常适合于存储这种类型的值
代码如下
[Visual Basic]
<%@ Import Namespace=SystemData %>
<HTML>
<HEAD>
<title>用于页面 UI 状态值的 ViewState/title>
</HEAD>
<body>
<form runat=server>
<H>
在 ViewState 中存储非控件状态
</H>
<P>
此示例将一列静态数据的当前排序顺序存储在 ViewState 中<br>
单击列标题中的链接可按该字段排序数据<br>
再次单击该链接将按相反顺序排序
<br><br><br>
<asp:datagrid id=DataGrid runat=server
OnSortCommand=SortGrid BorderStyle=None BorderWidth=px
BorderColor=#CCCCCC BackColor=White CellPadding= AllowSorting=True>
<HeaderStyle FontBold=True ForeColor=White
BackColor=#>
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<script runat=server>
在 ViewState 中跟蹤 SortField 属性
Property SortField() As String
Get
Dim o As Object = ViewState(SortField)
If o Is Nothing Then
Return StringEmpty
End If
Return CStr(o)
End Get
Set(Value As String)
If Value = SortField Then
与当前排序文件相同切换排序方向
SortAscending = Not SortAscending
End If
ViewState(SortField) = Value
End Set
End Property
在 ViewState 中跟蹤 SortAscending 属性
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 MyBaseLoad
If Not PageIsPostBack Then
BindGrid()
End If
End Sub
Sub BindGrid()
获取数据
Dim ds As New DataSet()
dsReadXml(ServerMapPath(TestDataxml))
Dim dv As New DataView(dsTables())
应用排序过滤器和方向
dvSort = SortField
If Not SortAscending Then
dvSort += DESC
End If
绑定网格
DataGridDataSource = dv
DataGridDataBind()
End Sub
Private Sub SortGrid(sender As Object e As DataGridSortCommandEventArgs)
DataGridCurrentPageIndex =
SortField = eSortExpression
BindGrid()
End Sub
</script>
[C#]
<%@ Page Language=C# %>
<%@ Import Namespace=SystemData %>
<HTML>
<HEAD>
<title>用于页面 UI 状态值的 ViewState</title>
</HEAD>
<body>
<form runat=server>
<H>
在 ViewState 中存储非控件状态
</H>
<P>
此示例将一列静态数据的当前排序顺序存储在 ViewState 中<br>
单击列标题中的链接可按该字段排序数据<br>
再次单击该链接将按相反顺序排序
<br><br><br>
<asp:datagrid id=DataGrid runat=server OnSortCommand=SortGrid
BorderStyle=None BorderWidth=px BorderColor=#CCCCCC
BackColor=White CellPadding= AllowSorting=True>
<HeaderStyle FontBold=True ForeColor=White BackColor=#>
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<script runat=server>
// 在 ViewState 中跟蹤 SortField 属性
string SortField {
get {
object o = ViewState[SortField];
if (o == null) {
return StringEmpty;
}
return (string)o;
}
set {
if (value == SortField) {
// 与当前排序文件相同切换排序方向
SortAscending = !SortAscending;
}
ViewState[SortField] = value;
}
}
// 在 ViewState 中跟蹤 SortAscending 属性
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 (!PageIsPostBack) {
BindGrid();
}
}
void BindGrid() {
// 获取数据
DataSet ds = new DataSet();
dsReadXml(ServerMapPath(TestDataxml));
DataView dv = new DataView(dsTables[]);
// 应用排序过滤器和方向
dvSort = SortField;
if (!SortAscending) {
dvSort += DESC;
}
// 绑定网格
DataGridDataSource = dv;
DataGridDataBind();
}
void SortGrid(object sender DataGridSortCommandEventArgs e) {
DataGridCurrentPageIndex = ;
SortField = eSortExpression;
BindGrid();
}
</script>
下面是上述两个代码段中引用的 testdataxml 的代码
<?xml version= standalone=yes?>
<NewDataSet>
<Table>
<pub_id></pub_id>
<pub_name>New Moon Books</pub_name>
<city>Boston</city>
<state>MA</state>
<country>USA</country>
</Table>
<Table>
<pub_id></pub_id>
<pub_name>Binnet & Hardley</pub_name>
<city>Washington</city>
<state>DC</state>
<country>USA</country>
</Table>
<Table>
<pub_id></pub_id>
<pub_name>Algodata Infosystems</pub_name>
<city>Berkeley</city>
<state>CA</state>
<country>USA</country>
</Table>
<Table>
<pub_id></pub_id>
<pub_name>Five Lakes Publishing</pub_name>
<city>Chicago</city>
<state>IL</state>
<country>USA</country>
</Table>
<Table>
<pub_id></pub_id>
<pub_name>Ramona Publishers</pub_name>
<city>Dallas</city>
<state>TX</state>
<country>USA</country>
</Table>
<Table>
<pub_id></pub_id>
<pub_name>GGG&G</pub_name>
<city>Muenchen</city>
<country>Germany</country>
</Table>
<Table>
<pub_id></pub_id>
<pub_name>Scootney Books</pub_name>
<city>New York</city>
<state>NY</state>
<country>USA</country>
</Table>
<Table>
<pub_id></pub_id>
<pub_name>Lucerne Publishing</pub_name>
<city>Paris</city>
<country>France</country>
</Table>
</NewDataSet>