ASP.NET ViewState 初探 (2) 转自msdn
2024-07-10 12:58:32
供稿:网友
请看下面的示例:要在 web 页上显示一个项目列表,而每个用户需要不同的列表排序。项目列表是静态的,因此可以将这些页面绑定到相同的缓存数据集,而排序顺序只是用户特定的 ui 状态的一小部分。viewstate 非常适合于存储这种类型的值。代码如下:
[visual basic]
<%@ 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">
' 在 viewstate 中跟踪 sortfield 属性
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
' 与当前排序文件相同,切换排序方向
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 mybase.load
if not page.ispostback then
bindgrid()
end if
end sub
sub bindgrid()
' 获取数据
dim ds as new dataset()
ds.readxml(server.mappath("testdata.xml"))
dim dv as new dataview(ds.tables(0))
' 应用排序过滤器和方向
dv.sort = sortfield
if not sortascending then
dv.sort += " desc"
end if
' 绑定网格
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>用于页面 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">
// 在 viewstate 中跟踪 sortfield 属性
string sortfield {
get {
object o = viewstate["sortfield"];
if (o == null) {
return string.empty;
}
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 (!page.ispostback) {
bindgrid();
}
}
void bindgrid() {
// 获取数据
dataset ds = new dataset();
ds.readxml(server.mappath("testdata.xml"));
dataview dv = new dataview(ds.tables[0]);
// 应用排序过滤器和方向
dv.sort = sortfield;
if (!sortascending) {
dv.sort += " desc";
}
// 绑定网格
datagrid1.datasource = dv;
datagrid1.databind();
}
void sortgrid(object sender, datagridsortcommandeventargs e) {
datagrid1.currentpageindex = 0;
sortfield = e.sortexpression;
bindgrid();
}
</script>
下面是上述两个代码段中引用的 testdata.xml 的代码:
<?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>