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

ASP.NET
通过数据捆绑将数据添加到ASP.NET 页面
ASP.NET 2.0程序安全的基础知识
ASP.NET2.0的跨页回调
使用ASP.Net Forms模式实现WebService身份验证
asp.net 2.0中不同web控件之间的相互调用
如何于DataGridView控件中以跨数据行方式显示数据
图片地址防盗链,通过IHttpHandler实现
ACCESS在Web.config里设置连接字符串
asp.net 2.0 下的表单验证Cookieless属性
结合ASP.NET与JavaScript开发电子沙盘
理解ASP.NET与客户端缓存之HTTP协议
数据回发时,维护ASP.NET Tree控件位置
获得DataGridViewCheckBoxColumn的状态
GridView显示主细表并添加打开、关闭功能
ASP.NET 2.0防止同一用户同时登陆
asp.net中对数据库表插入null空值的问题
IIS运行不了ASP.NET的解决办法
ASP.NET实现投票结果的图片进度条显示
实例:asp.net生成曲线图的过程
ASP.NET2.0中TextBox的两个有趣的属性

ASP.NET ViewState 初探 (2)


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