国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET ViewState 初探 (2) 轉自msdn

2024-07-10 12:58:32
字體:
來源:轉載
供稿:網友
請看下面的示例:要在 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>
    <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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富顺县| 玉门市| 元江| 临沂市| 昌黎县| 策勒县| 德阳市| 陆川县| 禹城市| 师宗县| 昌江| 兰考县| 个旧市| 朝阳区| 平遥县| 龙井市| 大邑县| 太白县| 女性| 宁晋县| 资阳市| 乡城县| 尚志市| 修文县| 马边| 武夷山市| 保亭| 丰都县| 嘉兴市| 河池市| 都江堰市| 同心县| 松桃| 绥阳县| 金堂县| 信阳市| 拜城县| 黄骅市| 茂名市| 合作市| 北流市|