设为首页 加入收藏 网站搜索 繁體中文 中国建站网 — 站长资源分享平台

ASP.NET ViewState 初探 (2)

来源:网络收集 作者:佚名 时间:2007-04-25 22:51:55

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

[Visual Basic]
<%@ 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 属性
Property SortField() As String

Get
Dim o As Object = ViewState("SortField")
If o Is Nothing Then
Return String.Empty
End If
Return CStr(o)
End Get

Set(Value As String)
If Value = SortField Then
' 与当前排序文件相同,切换排序方向
SortAscending = Not SortAscending
End If
ViewState("SortField") = Value
End Set

End Property

' 在 ViewState 中跟踪 SortAscending 属性
Property SortAscending() As Boolean

Get
Dim o As Object = ViewState("SortAscending")
If o Is Nothing Then
Return True
End If
Return CBool(o)
End Get

Set(Value As Boolean)
ViewState("SortAscending") = Value
End Set

End Property

Private Sub Page_Load(sender As Object, e As EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
BindGrid()
End If

End Sub

Sub BindGrid()

' 获取数据
Dim ds As New DataSet()
ds.ReadXml(Server.MapPath("TestData.xml"))

Dim dv As New DataView(ds.Tables(0))

' 应用排序过滤器和方向
dv.Sort = SortField
If Not SortAscending Then
dv.Sort += " DESC"
End If

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

End Sub

Private Sub SortGrid(sender As Object, e As DataGridSortCommandEventArgs)
DataGrid1.CurrentPageIndex = 0
SortField = e.SortExpression
BindGrid()
End Sub

</script>

[C#]
<%@ 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>

[1] [2]  下一页

Tags:

  • 好的评价 如果您觉得好,就请您
      0%(0)
  • 差的评价 如果您觉得差,就请您
      0%(0)
  • 相关文章
    广告赞助

    文章随便看看 设计素材 建站学院 网页模板 视频教程

    网友评论

    共有 0 位网友发表了评论,得分 0 分,平均 0 分    查看完整评论

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 1分

    内 容:

             通知管理员 验证码: