感谢您使用微软产品。您是指在UserControl(ascx文件)中使用Session变量么?您可以在您的UserControl中使用您的Session变量,不过由于UserControl并不存在实体,所以您要再使用前进行判断。具体的信息,建议您参阅下面的帖子:http://www.csdn.net/expert/topic/931/931924.xml?temp=.8769953希望对您有所帮助。
 
-微软全球技术中心  -zgh
 
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。

解决方案 »

  1.   

    谢谢zgh_ms([微软] 百变金刚),不过我是在自定义控件中不是在UserControl,自定义控件好象并不存在Session属性。http://www.csdn.net/expert/topic/931/931924.xml?temp=.8769953
    中的方法Session我改成Page.Session[""]试了一下,还是不行。
    能抽空再指点一下吗?
      

  2.   

    我试过了,在自定义控件里不能用Session,只能用ViewState
      

  3.   

    感谢您的回复。根据您的回复,我在VS.NET中新建一个WebApplication并且加入下面的UserControl, 编译并在WebForm1.aspx中使用,没有任何问题。下面是我使用Session的代码。请您注意,下面的语句是必需的:if ( null!= HttpContext.Current   &&   null!=Session["MySession"])//////////////////////////////////////////////////////////////////////////////////////////
     public abstract class WebUserControl1 : System.Web.UI.UserControl
     {  private void Page_Load(object sender, System.EventArgs e)
      {
       
       if ( null!= HttpContext.Current   &&   null!=Session["MySession"])
       {
        this.Response.Write(Session["MySession"]);
       }   // Put user code to initialize the page here
      }
    }//////////////////////////////////////////////////////////////////////////////////////////再次感谢您使用微软产品。-微软全球技术中心  -zgh本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
      

  4.   

    再次感谢zgh_ms([微软] 百变金刚),不过我的控件是继承的WebControl,在该控件里,null!=Session["MySession"]编译有问题。
    不过我这样试了一下,好象可以。
    加入:
         HttpContext.Current.Session["name"] = "username";
    取出:
         string username = HttpContext.Current.Session["name"].ToString();
    这样做不知道有什么别的影响没有?