同一个页面中用ViewState也可以呀,因为原来看到过这样的方法可以的
解决方案 »
- 问个问题--------缓存依赖项!
- 以代码和aspx页分离方式编写asp.net,生成的网站(以DLL方式),一定要建立虚拟目录吗?
- gridview里放了<asp:Image>小的图片可以显示, 大图片显示不了,这是为什么呢?
- FormView与GridView使用问题
- 请问如何在.NET中用IBE方式实现国内机票查询
- .net 树型控件问题!
- ASP.NET中的日历控件的问题!
- 请问如何用命令使得DataList中的image控件的visible=false?
- asp.net中有没有可能动态地为<body>的onload事件注册一段javascript程序?
- 杭州用asp.net的真的这么少吗?
- 怎样获得datagrid中的行数?
- 请教孟子等高手!如何得到我想要得xml文件
<%@ 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>