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

ASP.NET
ASP.NET中Session丢失原因与解决方案小结
.net开发中的一些注意事项及小技巧
学习Asp.Net经常会用到的函数集
在.net App中集成COM组件的一些简单技巧
彻底放弃IIS让Apache也支持ASP.NET
[JS.IntelliSense]VS2007(Orcas) So Cool
Asp.net 2.0 ViewState原理
asp.net ajax 使用updatepanel进行更新后的提示
动态代理DynamicProxy 介绍
您可能不知道的.Net2.0小技巧
Asp.Net2.0技巧(续)
“黑盒测试管理”以外的编程经验片断
实例开发:ASP.NET创建网络相册
封装stream,在读写stream时提供事件通知
GIS开发随笔--GIS技术的一点理解和MapNet控件试验
利用隐藏帧打印url的方法比较
无刷新仿google波形扭曲彩色Asp.net验证码
存储过程编写经验和优化措施
编程技巧:.Net Framework
编程技巧OOPs:复制构造函数

ASP.NET ViewState 初探 (2)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 102 ::
收藏到网摘: 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>