当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET ViewState 初探 (2)

ASP.NET
C# Quoted-Printable编码、解码
asp.net Google的translate工具翻译 API
.NET读取所有目录下文件正则匹配文本电子邮件
asp.net 半角全角转化工具
AjaxControlToolKit 显示浏览者本地语言的方法
asp.net HTML文件上传标签
ASP.NET 绑定DataSet中的多个表
ASP.NET 重定向的几种方法小结
Asp.Net 重定向必须要知道的一些资料
ASP.NET 导出到Excel时保留换行的代码
Asp.Net Cache缓存使用代码
document.getElementsByName和document.getElementById 在IE与FF中不同实现
GridView单元格合并
asp.net 大文件上传控件
asp.net 日期函数 某月的第一天和最后一天的日期
asp.net 时间类 一周的周一和周末的日期
C# javaScript函数的相互调用
asp.net membership 密码重设
"虚拟路径"..."映射到另一个应用程序,这是不允许的!
如何传值在2个页面之间 要求不刷新父页面,并且不能用Querystring传值

ASP.NET ViewState 初探 (2)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 146 ::
收藏到网摘: n/a

请看下面的示例:要在Web页上显示一个项目列表,而每个用户需要不同的列表排序。项目列表是静态的,因此可以将这些页面绑定到相同的缓存数据集,而排序顺序只是用户特定的UI状态的一小部分。ViewState非常适合于存储这种类型的值。代码如下:

[VisualBasic]


<%@ImportNamespace="System.Data"%>
<HTML>
<HEAD>
<title>用于页面UI状态值的ViewState/title>
</HEAD>
<body>
<formrunat="server">
<H3>
在ViewState中存储非控件状态
</H3>
<P>
此示例将一列静态数据的当前排序顺序存储在ViewState中。<br>
单击列标题中的链接,可按该字段排序数据。<br>
再次单击该链接,将按相反顺序排序。
<br><br><br>
<asp:datagridid="DataGrid1"runat="server"
OnSortCommand="SortGrid"BorderStyle="None"BorderWidth="1px"
BorderColor="#CCCCCC"BackColor="White"CellPadding="5"AllowSorting="True">
<HeaderStyleFont-Bold="True"ForeColor="White"
BackColor="#006699">
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<scriptrunat="server">

'在ViewState中跟踪SortField属性
PropertySortField()AsString

Get
DimoAsObject=ViewState("SortField")
IfoIsNothingThen
ReturnString.Empty
EndIf
ReturnCStr(o)
EndGet

Set(ValueAsString)
IfValue=SortFieldThen
'与当前排序文件相同,切换排序方向
SortAscending=NotSortAscending
EndIf
ViewState("SortField")=Value
EndSet

EndProperty

'在ViewState中跟踪SortAscending属性
PropertySortAscending()AsBoolean

Get
DimoAsObject=ViewState("SortAscending")
IfoIsNothingThen
ReturnTrue
EndIf
ReturnCBool(o)
EndGet

Set(ValueAsBoolean)
ViewState("SortAscending")=Value
EndSet

EndProperty

PrivateSubPage_Load(senderAsObject,eAsEventArgs)HandlesMyBase.Load

IfNotPage.IsPostBackThen
BindGrid()
EndIf

EndSub

SubBindGrid()

'获取数据
DimdsAsNewDataSet()
ds.ReadXml(Server.MapPath("TestData.xml"))

DimdvAsNewDataView(ds.Tables(0))

'应用排序过滤器和方向
dv.Sort=SortField
IfNotSortAscendingThen
dv.Sort+="DESC"
EndIf

'绑定网格
DataGrid1.DataSource=dv
DataGrid1.DataBind()

EndSub

PrivateSubSortGrid(senderAsObject,eAsDataGridSortCommandEventArgs)
DataGrid1.CurrentPageIndex=0
SortField=e.SortExpression
BindGrid()
EndSub

</script>

[C#]


<%@PageLanguage="C#"%>
<%@ImportNamespace="System.Data"%>
<HTML>
<HEAD>
<title>用于页面UI状态值的ViewState</title>
</HEAD>
<body>
<formrunat="server">
<H3>
在ViewState中存储非控件状态
</H3>
<P>
此示例将一列静态数据的当前排序顺序存储在ViewState中。<br>
单击列标题中的链接,可按该字段排序数据。<br>
再次单击该链接,将按相反顺序排序。
<br><br><br>
<asp:datagridid="DataGrid1"runat="server"OnSortCommand="SortGrid"
BorderStyle="None"BorderWidth="1px"BorderColor="#CCCCCC"
BackColor="White"CellPadding="5"AllowSorting="True">
<HeaderStyleFont-Bold="True"ForeColor="White"BackColor="#006699">
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<scriptrunat="server">

//在ViewState中跟踪SortField属性
stringSortField{

get{
objecto=ViewState["SortField"];
if(o==null){
returnString.Empty;
}
return(string)o;
}

set{
if(value==SortField){
//与当前排序文件相同,切换排序方向
SortAscending=!SortAscending;
}
ViewState["SortField"]=value;
}
}

//在ViewState中跟踪SortAscending属性
boolSortAscending{

get{
objecto=ViewState["SortAscending"];
if(o==null){
returntrue;
}
return(bool)o;
}

set{
ViewState["SortAscending"]=value;
}
}

voidPage_Load(objectsender,EventArgse){

if(!Page.IsPostBack){
BindGrid();
}
}

voidBindGrid(){

//获取数据
DataSetds=newDataSet();
ds.ReadXml(Server.MapPath("TestData.xml"));

DataViewdv=newDataView(ds.Tables[0]);

//应用排序过滤器和方向
dv.Sort=SortField;
if(!SortAscending){
dv.Sort+="DESC";
}

//绑定网格
DataGrid1.DataSource=dv;
DataGrid1.DataBind();
}

voidSortGrid(objectsender,DataGridSortCommandEventArgse){

DataGrid1.CurrentPageIndex=0;
SortField=e.SortExpression;
BindGrid();
}

</script>

下面是上述两个代码段中引用的testdata.xml的代码:


<?xmlversion="1.0"standalone="yes"?>
<NewDataSet>
<Table>
<pub_id>0736</pub_id>
<pub_name>NewMoonBooks</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>AlgodataInfosystems</pub_name>
<city>Berkeley</city>
<state>CA</state>
<country>USA</country>
</Table>
<Table>
<pub_id>1622</pub_id>
<pub_name>FiveLakesPublishing</pub_name>
<city>Chicago</city>
<state>IL</state>
<country>USA</country>
</Table>
<Table>
<pub_id>1756</pub_id>
<pub_name>RamonaPublishers</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>ScootneyBooks</pub_name>
<city>NewYork</city>
<state>NY</state>
<country>USA</country>
</Table>
<Table>
<pub_id>9999</pub_id>
<pub_name>LucernePublishing</pub_name>
<city>Paris</city>
<country>France</country>
</Table>
</NewDataSet>