2014-03-27 170 views

回答

0

嘗試像這樣的:

string name= HttpContext.Current.Session["Name"].ToString(); 

string name=Session["Name"].ToString(); 
+0

不工作,其給予NullExc .. –

+0

顯示您的兩個頁面的一些代碼 –

-1

試試這個還有:

HttpContext.Current.Session["Name"] 
0

您需要從該控件創建一個對象。

0

沒有看到一點點的代碼,這將是很難說。

  • 也許會話超時
  • 是否有錯誤happing導致會話狀態是明確的,或註銷
  • 修改web.config文件或多個文件可能會導致應用程序重新啓動。

最後,我必須問: - 會話是否以某種方式被禁用?它在其他頁面之間工作嗎?也許它被禁用了Ajax請求? - 可能是一個魔術字符串錯誤。

個人,以避免魔術字符串錯誤,我使用會話包裝:

/// <summary> 
///  The session manager 
/// </summary> 

public sealed class SessionManager 
{ 
    #region ISessionManager Members 

    /// <summary> 
    ///  Clears the session. 
    /// </summary> 
    public void ClearSession() 
    { 
     HttpContext.Current.Session.Clear(); 
     HttpContext.Current.Session.Abandon(); //Abandon ends the entire session (the user gets a new SessionId) 
    } 

    /// <summary> 
    ///  Gets or sets the current employe. 
    /// </summary> 
    /// <value>The current employe.</value> 
    public EmployeDto CurrentEmploye 
    { 
     get { return Get<EmployeDto>(); } 
     set { Add(value); } 
    } 

    /// <summary> 
    ///  Gets or sets the parameters. 
    /// </summary> 
    /// <value>The parameters.</value> 
    public IList<ParameterDto> Parameters 
    { 
     get { return Get<List<ParameterDto>>() ?? new List<ParameterDto>(); } 
     set { Add(value); } 
    } 

    #endregion 

    #region Methods 

    /// <summary> 
    ///  Adds the specified key. 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    /// <param name="value">The value.</param> 
    /// <param name="key">The key.</param> 
    /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> 
    /// <exception cref="System.ArgumentNullException">key</exception> 
    /// <exception cref="System.Exception">Session elements cannot be added when session is disabled.</exception> 
    public static bool Add<T>(T value, [CallerMemberName] string key = null) 
    { 
     if (key == null) throw new ArgumentNullException("key"); 
     HttpContext current = HttpContext.Current; 

     if (current == null) 
     { 
      return false; 
     } 

     if (current.Session.Mode == SessionStateMode.Off) 
     { 
      throw new Exception("Session elements cannot be added when session is disabled."); 
     } 

     current.Session.Add(key, value); 

     return true; 
    } 

    /// <summary> 
    ///  Gets the specified key. 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    /// <param name="key">The key.</param> 
    /// <returns>``0.</returns> 
    /// <exception cref="System.ArgumentNullException">key</exception> 
    /// <exception cref="System.Exception">Session elements cannot be added when session is disabled.</exception> 
    public static T Get<T>([CallerMemberName] string key = null) where T : class 
    { 
     if (key == null) throw new ArgumentNullException("key"); 
     HttpContext current = HttpContext.Current; 

     if (current.Session.Mode == SessionStateMode.Off) 
     { 
      throw new Exception("Session elements cannot be added when session is disabled."); 
     } 

     return current.Session[key] as T; 
    } 

    /// <summary> 
    ///  Gets the specified key. 
    /// </summary> 
    /// <param name="key">The key.</param> 
    /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> 
    /// <exception cref="System.ArgumentNullException">key</exception> 
    /// <exception cref="System.Exception">Session elements cannot be added when session is disabled.</exception> 
    public static bool Get([CallerMemberName] string key = null) 
    { 
     if (key == null) throw new ArgumentNullException("key"); 
     HttpContext current = HttpContext.Current; 

     if (current.Session.Mode == SessionStateMode.Off) 
     { 
      throw new Exception("Session elements cannot be added when session is disabled."); 
     } 
     bool result = false; 
     bool.TryParse(current.Session[key].ToString(), out result); 
     return result; 
    } 

    /// <summary> 
    ///  Removes the specified key. 
    /// </summary> 
    /// <param name="key">The key.</param> 
    /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> 
    /// <exception cref="System.ArgumentNullException">key</exception> 
    /// <exception cref="System.Exception">Session elements cannot be added when session is disabled.</exception> 
    public static bool Remove(string key) 
    { 
     if (key == null) throw new ArgumentNullException("key"); 
     HttpContext current = HttpContext.Current; 

     if (current == null) 
     { 
      return false; 
     } 

     if (current.Session.Mode == SessionStateMode.Off) 
     { 
      throw new Exception("Session elements cannot be added when session is disabled."); 
     } 

     current.Session.Remove(key); 

     return true; 
    } 

    #endregion 
} 

我的示例使用反射來定義鍵名稱相同的屬性名,但你可以使用常數來代替。它還檢查會話是否被禁用,這可能有助於調試您的案例。