我在服务器上有一个变量,想让它保存某一个页面的值。public string username;
客户端
localhost.Service1() ts = new localhost.Service1();
ts.CookieContainer =  new System.Net.CookieContainer();
我把这两句放在构造函数或Page_Load里,可是每点击一次按钮,这两句都会被执行。
客户端一个按钮事件里调用setA("abc")
为什么另一个按钮里调用getA()却得不到abc?服务器端
[WebMethod (EnableSession=true)]
public string getA()
{
  return (string)Session["username"];
}
    
[WebMethod (EnableSession=true)]
public void setA(string Value)
{      
  Session["username"] = Value;
}

解决方案 »

  1.   

    1、找到VS.NET自动为你生成的代码(WebService包装)
    2、应该有这样一个类:
    public class YourServiceName : System.Web.Services.Protocols.SoapHttpClientProtocol
    3、在其构造函数内添加:
    this.CookieContainer = new CookieContainer();
      

  2.   

    或者:
    CookieContainer cc = new CookieContainer();
    ts.CookieContainer = cc;
    cc.Method1(......ts.CookieContainer = cc;
    cc.Method2(...保持对同一个CookieContainer引用
      

  3.   

    WebService一般是用Cookie在客户端保存值
      

  4.   

    或者:
    CookieContainer cc = new CookieContainer();
    ts.CookieContainer = cc;
    ts.Method1(......ts.CookieContainer = cc;
    ts.Method2(...保持对同一个CookieContainer的引用
      

  5.   

    public class YourServiceName : System.Web.Services.Protocols.SoapHttpClientProtocol
    不对呀。ServieceName应该是继承System.Web.Services.WebService
    里面没这个属性CookieContainer