前台代码:
  <a href="" id="n6">会员设置</a>
  <script language="javascript" type="text/javascript">$("#n6").click(function(){
  $.post("getsession.aspx",function(session){alert("fd");
    alert(session);
  });
  
});
</script>getsession.aspx :
  using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;public partial class UserControl_getsession : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(Session["username"]);    }
}为什么如此设置之后都没有反应呢?

解决方案 »

  1.   

    能不能先把Session转换为Json格式的呢?谁知道你的Session是什么格式的
      

  2.   

    你的    $.post("getsession.aspx",function(session){alert("fd");根本不是正常的asp.net回发,怎么可能取到Session数据呢?
    去使用asp.net ajax的UpdatePanel吧!或者干脆放弃Session。
      

  3.   

    他这个应该是Jquery的Ajax写法吧。。我也不清楚。。
      

  4.   

    不论是什么写法,不都可以看出到底是怎样请求服务器的嘛!因此完全可以一眼看出这类Ajax写法根本不可能使用到Session数据,也不可能触发后台事件等等一些需要利用状态的东西。
      

  5.   

    要看一个ajax框架是否能维护到session,至少要看到它维系了cookie的提交到服务器和客户端修改全过程;要看一个ajax框架是否能触发后台事件并准确反映控件值,至少要看到它维系了viewstate的提交到服务器和客户端修改全过程。如果没有,我们一眼就知道这类ajax只是适合最原始的没有session或者没有事件和无法保持控件值的东西。
      

  6.   

    通过简单测试,是可以弹出Session的。<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestJqureySession1.aspx.cs"
        Inherits="Pages_Office_StorageManager_TestJqureySession1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>    <script src="../../../js/JQuery/jquery_last.js" type="text/javascript"></script>    <script language="javascript" type="text/javascript">
            $(document).ready(function() {
                // Your code here...            $("#n6").click(function() {
                    alert('a');
                    $.post("TestJqureySession.aspx", function(session) {
                        alert(session);
                        document.getElementById('div1').innerHTML = session;
                    });            });
            });
        </script></head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="n6" type="button" value="Test" />
            <div id="div1"></div>
        </div>
        </form>
    </body>
    </html>
    另一个测试页面的后台:using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;public partial class Pages_Office_StorageManager_TestJqureySession : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string test = "我只是测试而已Test";
            Session["test"] = test;
            Response.Write(Session["Test"].ToString());
        }
    }
      

  7.   

    ASP.Net_SessionID 这个 HTTP Header就是 ASP.Net默认的 Session, 因为它在设置的时候使用了 HttpOnly属性,所以不能直接在 document.cookies中获取到  不过你可以修改一下你服务端代码, 去掉  ASP.Net_SessionID 的 HttpCookie.HttpOnly = false那么 在JS代码中就可以获得到了
    =====================ASP.Net_SessionID 这个 HTTP Header 对于同一Domain发送的请求都是带了的,你没取到应该是服务端的问题,试试用HttpFox监听一下链接,看此头是否发送过去了。 你的服务端使用的是HttpHandler? 
      

  8.   

    可能我说的跟sp1234大哥说的不是同一回事。。我说的是要转换为Json(string)。。而你说的Session可能说的是直接Session对象?
      

  9.   

    你可以先把session放到hidden里,再在js中取hidden的值
      

  10.   

    小虎哥,你做的那个测试肯定是可以的啊。Response.Write(Session["Test"].ToString());都已经变成字符串输出了jquery当然能取到了
      

  11.   

    哦,我刚刚google了一下“jquery是否维护cookie”没有什么答案。别人说可以只是单向单向传送cookie。如果可以正确传送,那么asp.net大致应该是可以刚好访问到Session数据的。那么就只是返回值的写法问题了。
      

  12.   

    前台改用$.get试试呢?
    后台Response.ClearContent();
    Response.Write(Session["username"].ToString());
    Response.End();
    另外你不必Response.Cache.SetCacheability(HttpCacheability.NoCache);可以用$.ajax()设置cache:false参数
      

  13.   

    你说了这么多我不是很懂,但是通过ajax调用.ashx来实现调用session是可以实现(js调用session的)
      

  14.   

    哦,sorry,我查了一下,所有Post默认地都传递cookie(尽管并不真正维护修改本地cookie)。既然如此,我前边的观点应该修改为:恰好可以告诉后台session,更多的做不了。不过这也够了。我写了一个最简单的demo(干嘛要用jquery那种东西呢?)<%@ Page Language="C#" %><script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Session["pi"] = "中国人民银行";
        }
    </script>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/WebService.asmx" />
            </Services>
        </asp:ScriptManager>
        <span onclick="WebService.HelloWorld(function(result){alert(result);});">Click Me</span>
        </form>
    </body>
    </html>可见,直接调用web service就可以了。而这个web service则只是:<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>using System.Web.Services;[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService {    [WebMethod(EnableSession=true)]
        public string HelloWorld() {
            return Session["pi"] as string;
        }
    }
      

  15.   

    轻量级的ajax只是单方面传递cookie,而不会在客户端维护cookie的改变,当然也不会维护viewstate的改变。我今后会更准确地这样来描述。asp.net ajax可以轻松地访问web service,它的语法也非常简单直观。如上例中span的onclick属性。
      

  16.   

    楼上sp1234大哥,看到看到webservice,我想起来几个东西,特向你请教:
    1.最近在用Linq to Entity,哇塞,相比以前的SQL数据库编程和js编程效率提高的可不仅一倍。。
      你对Linq to Entity评价怎么样?
    2.在.net的微软企业库中,AOP处理异常,缓存,日志等东西,可经常用?
      

  17.   

    10楼的代码, 在服务器脚本里接受得到form提交过来的数据吗?
      

  18.   

    可以把session的值放入到Hiddenfield中,然后再提取。
      

  19.   

    把楼主的代码改一下, 这样有输出什么. . .<a href="" id="n6">会员设置</a>
    <script language="javascript" type="text/javascript">$("#n6").click(function(){
    $.post("getsession.aspx?test=go",function(session){alert("fd");
    alert(session);
    });});
    </script>getsession.aspx :
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;public partial class UserControl_getsession : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
       if(Request.QueryString["test"] == "go")
       {
           Response.Cache.SetCacheability(HttpCacheability.NoCache);
           Response.Write(Session["username"].ToString());  //得保证这个Session["username"]有值
       }
       else
       {
           Response.Write("请求失败。。");
       }}
    }
      

  20.   


    因为我用在Linq发布之前就自己写ORM系统,后来Linq发布终于解决了我一个一直没有很好解决的通用查询机制问题,所以我对Linq to xxxx 都包装成实现我的接口的东西。在帖子
    http://topic.csdn.net/u/20100312/11/c5b51fa3-f1e7-4c46-aa74-14aad1cf1a0e.html?31209
    17楼我介绍了我的接口规格。从我期望的角度,Linq to Entity应用起来似乎还是太复杂了。微软企业库我从来不用。但是HttpRuntime.Cache我经常用。AOP过于随意。实际上我们可以自定义事件,或者使用INotifyPropertyChanged、INotifyPropertyChanged等等方法来在设计时明明白白地告诉别人我们的架构欢迎别人来注入监听机制,这样可以预知被人修改后的后果。而不需要隐式支持危险的AOP。日志在测试时经常使用,测试完就可以关闭。这几乎没有什么好说的。是非常必须的东西。但是实现方法可以不一。我有时候仅写一行代码  File.AppendAllText(file,content) 就记录日志了。
      

  21.   

    个人理解就是在asp.net的页面XX.aspx中获得session的值。 搞这么乱干什么呢