0

我有一个带有 js onClick 功能的按钮。单击按钮时,我想保存一个值,以便在进入新页面后可以从后面的代码中读取它。我熟悉后面代码中的 Session[] 变量和客户端的 SessionStorage,但不熟悉它们之间的共享方式。

我想我在问如何从 js 函数中保存一个变量,以便稍后在页面的后面代码中读取。

  <script "text/javascript">

           $('.toggle a').click(function () {
               var select = $(this);

                   if (select.hasClass("active")) {                      
                   var newValue = "Wow!"
                   //save newValue into test
                   alert('<%= Session["test"] %>');
                   window.location.assign("Contact.aspx");

               }else
                   select.parents('li').toggleClass('is-open');

           });

//后面的代码 Site.Master.cs

    `using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;


    namespace WebApplication6{


public partial class SiteMaster : MasterPage
{
    private const string AntiXsrfTokenKey = "__AntiXsrfToken";
    private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
    private string _antiXsrfTokenValue;

    protected void Page_Init(object sender, EventArgs e)
    {


    }


    protected void master_Page_PreLoad(object sender, EventArgs e)
    {

    }

    protected void Page_Load(object sender, EventArgs e)
    {

        //if (navTree.Nodes.Count != 0) return;
        TreeView navTree = new TreeView();
        Service1 myService = new Service1();

        //Use a gridview to store the table data before building the menu
        GridView sites = new GridView();

        sites.DataSource = myService.GetAllSites();
        sites.DataBind();




        //After the gridview is filled iterate through rows, adding new nodes
        //for each site and children for each rule
        foreach (GridViewRow siteRow in sites.Rows)
        {


            String siteName = siteRow.Cells[1].Text;

            TreeNode existingNode = isParent(siteName, navTree);
            if (existingNode == null)
            {
                TreeNode ParentNode = new TreeNode(siteRow.Cells[1].Text);
                ParentNode.SelectAction = TreeNodeSelectAction.Expand;
                ParentNode.Collapse();
                navTree.Nodes.Add(ParentNode);

                TreeNode ChildNode = new TreeNode(siteRow.Cells[2].Text);
                ChildNode.NavigateUrl = "http://gamespot.com";
                ParentNode.ChildNodes.Add(ChildNode);
            }
            else
            {
                TreeNode ChildNode = new TreeNode(siteRow.Cells[2].Text);
                ChildNode.NavigateUrl = "http://kotaku.com";
                existingNode.ChildNodes.Add(ChildNode);
            }

        }

        createMenu(navTree);

    }




    }

    [WebMethod(EnableSession = true)]
    public static void SetSessionValue(string sessionValue)
    {
        HttpContext.Current.Session["test"] = sessionValue;

    }




}

}

4

1 回答 1

3

好吧,在没有看到您尝试过的情况下,我建议使用 ASP.NET AJAX 页面方法作为客户端会话值和将其存储在 ASP.NETSession缓存中的管道,如下所示:

客户端:

$.ajax({
    type: "POST",
    url: "YourPage.aspx/StoreSessionValue",
    data: {"sessionValue": "theSessionValue"},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        alert("Successfully save session value.");
    }
});

服务器端(YourPage.aspx):

[WebMethod(EnableSession = true)]
public static void StoreSessionValue(string sessionValue)
{
    HttpContext.Current.Session["TheSessionValue"] = sessionValue;
}

现在,在您的正常 ASP.NET 页面生命周期中,您可以访问该Session值,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if(Session["TheSessionValue"] != null)
    {
        string theSessionValue = Session["TheSessionValue"] as string;

        // Do something with or based upon the session value here
    }
}

更新:

将您的 JavaScript 更改为:

<script "text/javascript">
   $('.toggle a').click(function () {
       var select = $(this);

       if (select.hasClass("active")) {                      
           var newValue = "Wow!"
           //save newValue into test
           $.ajax({
               type: "POST",
               url: "YourPage.aspx/StoreSessionValue",
               data: {"sessionValue": "theSessionValue"},
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function(msg) {
                   alert("Successfully save session value.");
                   window.location.assign("Contact.aspx");
               }
           });
           //alert('<%= Session["test"] %>');

       }else
           select.parents('li').toggleClass('is-open');
   });

注意:重命名YourPage.aspx/StoreSessionValue为您的页面名称和 Web 方法名称。

于 2013-08-09T01:33:04.793 回答