请看下面的示例:要在 Web 页上显示一个项目列表,而每个用户需要不同的列表排序。项目列表是静态的,因此可以将这些页面绑定到相同的缓存数据集,而排序顺序只是用户特定的 UI 状态的一小部分。ViewState 非常适合于存储这种类型的值。代码如下: [Visual Basic] 在 ViewState 中跟踪 SortField 属性 Get Set(Value As String) End Property 在 ViewState 中跟踪 SortAscending 属性 Get Set(Value As Boolean) End Property Private Sub Page_Load(sender As Object, e As EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then End Sub Sub BindGrid() 获取数据 Dim dv As New DataView(ds.Tables(0)) 应用排序过滤器和方向 绑定网格 End Sub Private Sub SortGrid(sender As Object, e As DataGridSortCommandEventArgs) </script> [C#] // 在 ViewState 中跟踪 SortField 属性 get { set { // 在 ViewState 中跟踪 SortAscending 属性 get { set { void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { void BindGrid() { // 获取数据 DataView dv = new DataView(ds.Tables[0]); // 应用排序过滤器和方向 // 绑定网格 void SortGrid(object sender, DataGridSortCommandEventArgs e) { DataGrid1.CurrentPageIndex = 0; </script> 下面是上述两个代码段中引用的 testdata.xml 的代码: <?xml version=”1.0″ standalone=”yes”?>
<%@ Import Namespace=”System.Data” %>
<HTML>
<HEAD>
<title>用于页面 UI 状态值的 ViewState/title>
</HEAD>
<body>
<form runat=”server”>
<H3>
在 ViewState 中存储非控件状态
</H3>
<P>
此示例将一列静态数据的当前排序顺序存储在 ViewState 中。<br>
单击列标题中的链接,可按该字段排序数据。<br>
再次单击该链接,将按相反顺序排序。
<br><br><br>
<asp:datagrid id=”DataGrid1″ runat=”server”
OnSortCommand=”SortGrid” BorderStyle=”None” BorderWidth=”1px”
BorderColor=”#CCCCCC” BackColor=”White” CellPadding=”5″ AllowSorting=”True”>
<HeaderStyle Font-Bold=”True” ForeColor=”White”
BackColor=”#006699″>
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<script runat=”server”>
Property SortField() As String
Dim o As Object = ViewState(“SortField”)
If o Is Nothing Then
Return String.Empty
End If
Return CStr(o)
End Get
If Value = SortField Then
与当前排序文件相同,切换排序方向
SortAscending = Not SortAscending
End If
ViewState(“SortField”) = Value
End Set
Property SortAscending() As Boolean
Dim o As Object = ViewState(“SortAscending”)
If o Is Nothing Then
Return True
End If
Return CBool(o)
End Get
ViewState(“SortAscending”) = Value
End Set
BindGrid()
End If
Dim ds As New DataSet()
ds.ReadXml(Server.MapPath(“TestData.xml”))
dv.Sort = SortField
If Not SortAscending Then
dv.Sort += ” DESC”
End If
DataGrid1.DataSource = dv
DataGrid1.DataBind()
DataGrid1.CurrentPageIndex = 0
SortField = e.SortExpression
BindGrid()
End Sub
<%@ Page Language=”C#” %>
<%@ Import Namespace=”System.Data” %>
<HTML>
<HEAD>
<title>用于页面 UI 状态值的 ViewState</title>
</HEAD>
<body>
<form runat=”server”>
<H3>
在 ViewState 中存储非控件状态
</H3>
<P>
此示例将一列静态数据的当前排序顺序存储在 ViewState 中。<br>
单击列标题中的链接,可按该字段排序数据。<br>
再次单击该链接,将按相反顺序排序。
<br><br><br>
<asp:datagrid id=”DataGrid1″ runat=”server” OnSortCommand=”SortGrid”
BorderStyle=”None” BorderWidth=”1px” BorderColor=”#CCCCCC”
BackColor=”White” CellPadding=”5″ AllowSorting=”True”>
<HeaderStyle Font-Bold=”True” ForeColor=”White” BackColor=”#006699″>
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<script runat=”server”>
string SortField {
object o = ViewState[“SortField”];
if (o == null) {
return String.Empty;
}
return (string)o;
}
if (value == SortField) {
// 与当前排序文件相同,切换排序方向
SortAscending = !SortAscending;
}
ViewState[“SortField”] = value;
}
}
bool SortAscending {
object o = ViewState[“SortAscending”];
if (o == null) {
return true;
}
return (bool)o;
}
ViewState[“SortAscending”] = value;
}
}
BindGrid();
}
}
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(“TestData.xml”));
dv.Sort = SortField;
if (!SortAscending) {
dv.Sort += ” DESC”;
}
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
SortField = e.SortExpression;
BindGrid();
}
<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>
asp.net viewstate 初探 (2)_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » asp.net viewstate 初探 (2)_asp.net技巧
相关推荐
- 暂无文章
