日期:2014-05-18  浏览次数:20512 次

请问一个项目中遇到的关于Session的问题,

主页面中的点击事件:
protected   void   lbAddCadre_Click(object   sender,   EventArgs   e)
{

        AddFlipWindowPlaceHolder.ControlPath   =   "AddEmployee.ascx ";
        _currentSelect   =   "divAddCadre ";
        Session.Add( "addEmployee ",   "addEmployee ");

}

用户控件中的点击提交事件:
protected   void   btnAdd_Command(object   sender,   CommandEventArgs   e)
{
        if   (Session[ "addEmployee "].ToString()   ==   "addEmployee ")
        {
                ......
                ......
                CustomPlaceHolder   cph   =   (CustomPlaceHolder)this.Parent;
                cph.ControlPath   =   null;
                if(Session   !=   null)
                        Session.Remove( "addEmployee ");
}


异常详细信息:   System.NullReferenceException:   未将对象引用设置到对象的实例

行   130:                         CustomPlaceHolder   cph   =   (CustomPlaceHolder)this.Parent;
行   131:                         cph.ControlPath   =   null;
行   132:                         if(Session   !=   null)         //此处为红色提示错误
行   133:                         Session.Remove( "addEmployee ");

堆栈跟踪:  


[NullReferenceException:   未将对象引用设置到对象的实例。]
      System.Web.UI.UserControl.get_Session()   +29
      AddEmployee.btnAdd_Command(Object   sender,   CommandEventArgs   e)   in   f:\MIC2.2\AddEmployee.ascx.cs:132
      System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs   e)   +96
      System.Web.UI.WebControls.Button.RaisePostBackEvent(String   eventArgument)   +177
      System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String   eventArgument)   +31
      System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler   sourceControl,   String   eventArgument)   +32
      System.Web.UI.Page.RaisePostBackEvent(NameValueCollection   postData)   +72
      System.Web.UI.Page.ProcessRequestMain(Boolean   includeStagesBeforeAsyncPoint,   Boolean   includeStagesAfterAsyncPoint)   +3838

 




------解决方案--------------------
Session[ "... "]为正确的格式;
单写Session没有引用对象;
------解决方案--------------------
Session.Add( "addEmployee ", "addEmployee ");

没有这样写过

Session[ "addEmployee "]= "addEmplo