zoukankan      html  css  js  c++  java
  • Using PageMethods to access Session data

    Using PageMethods to access Session data

    Here is one of the samples I was asked for at TechEd last week.  How to access session state data from the browser.  There are two C# methods in the page.  One overrides the the OnLoad method and stores a value in session state for demonstration purposes.  The other is a static method decorated with the WebMethod attribute that can be called from JavaScript. It retrieves the value for the given key from session state.  Currently, only static methods are callable use the PageMethods object in the browser.

    In the JavaScript code, there is a pageLoad method which will be called automatically by the ASP.NET AJAX script library.  The PageMethods object is used to invoke the method on the server.  It provides callbacks for success and error.  The success callback just displays the value retrieved from session state on the server.

    <%@ Import Namespace="System.Web.Services" %>
    <script runat="server" language="C#">
    protected override void OnLoad(EventArgs e) {
        HttpContext.Current.Session["foo"] = "bar";
    }

    [WebMethod]
    public static string Session(string key) {
        return (string)HttpContext.Current.Session[key];
    }
    </script>

    <
    script type="text/javascript">
    function
    pageLoad(sender, arg) {
        PageMethods.Session(
    "foo", OnCallComplete, OnCallError);
    }

    function OnCallComplete(result, userContext, methodName) {
        alert(result);
    }

    function OnCallError(error, userContext, methodName) {
        if(error !== null) {
            alert(error.get_message());
        }
    }
    </script>
    <
    form runat="server">
    <asp:scriptmanager runat="server" id="scriptmanager" />
    </
    form>

  • 相关阅读:
    国内公有云对比(1)- 功能篇
    国内公有云对比(1.1)- 功能篇之新浪云
    人不成熟的六大特征
    HDU1506 Largest Rectangle in a Histogram (动规)
    windows pipe
    LeetCode || Candy
    Spin.js-CSS动画进度载入器
    poj 2932 Coneology (扫描线)
    虚拟存储器--虚拟地址与物理地址
    eclipse下的ssh框架整合过程及測试
  • 原文地址:https://www.cnblogs.com/zwei1121/p/757027.html
Copyright © 2011-2022 走看看