欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【ASP.NET ViewState 初探 (2)】,下面是详细的分享!
ASP.NET ViewState 初探 (2)
<%@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>
<%@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>
<?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>
以上所分享的是关于ASP.NET ViewState 初探 (2),下面是编辑为你推荐的有价值的用户互动:
相关问题:asp.net viewstate的问题
答: 就是在Html的 page里 加上 EnableViewState="false" 这样可以减少一些乱码 因为你在前台页面用了 服务器空间 所以只能尽量减少乱码 不能消除 除非全部用html控件代替 >>详细
相关问题:ASP.NET 使用AspNetPager进行分页,查询条件丢失问题
答:用ViewState将查询条件保存起来,每次分页的时候检查ViewState是否为空,如果不为空就把里面的查询条件取出来执行查询,像这样 //比如你已经建立了一个ViewStateViewState["conditon"] = null;//如果有查询,就将查询条件保存到ViewStateViewSta... >>详细
相关问题:asp.net中html
答:这是在aspx页面中输入 aspx.cs 中的信息 你在cs文件中定义一个 protected string ggods=null; page_load方法中 定义 ggods=“110”; 页面中就会出现110字符串 手打求采纳 >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
