用于页面 UI 状态值的 ViewState 排序

=======default.aspx=========                    

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<html>
<head>
    <title>用于页面 UI 状态值的 ViewState</title>
</head>
<body>
    <form id="Form1" 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 &amp; 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&amp;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>

 

 

 

转载于:https://www.cnblogs.com/pchgo/articles/1267780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值