0
A
回答
0
嘗試像這樣的:
string name= HttpContext.Current.Session["Name"].ToString();
或
string name=Session["Name"].ToString();
-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
}
我的示例使用反射來定義鍵名稱相同的屬性名,但你可以使用常數來代替。它還檢查會話是否被禁用,這可能有助於調試您的案例。
相關問題
- 1. 訪問會話變量8
- 2. JSTL:訪問會話變量
- 3. 訪問會話變量
- 4. 無法調用訪問會話變量
- 5. 使用會話變量訪問頁面
- 6. 使用javascript訪問vbscript會話變量
- 7. Rails:如何使用多個控制器訪問會話變量?
- 8. 會話變量爲用戶名
- 9. 使用會話變量填充用戶控件TextBox
- 10. 在servlet外部訪問會話變量
- 11. 從jQuery訪問會話變量
- 12. Spring框架訪問會話變量
- 13. 模型訪問會話變量?
- 14. 從JavaScript訪問ASP.NET會話變量
- 15. Spring MVC 3.0訪問會話變量
- 16. 如何訪問會話變量在Laravel
- 17. 在JavaScript中訪問會話變量
- 18. 無法訪問會話變量
- 19. 來自HTTPModule的訪問會話變量
- 20. 從webroot訪問cakephp會話變量
- 21. 類中的訪問會話變量
- 22. ASP.NET MVC3會話變量不可訪問
- 23. 在Nginx中訪問會話變量
- 24. 訪問來自JavaScript的會話變量
- 25. 從類擴展訪問會話變量
- 26. 從WebView訪問會話變量
- 27. PHP會話變量不可訪問
- 28. RSpec spec_helper訪問會話變量
- 29. ASP.NET:訪問會話變量global.asax
- 30. 如何在用戶控件卸載後清除會話變量
不工作,其給予NullExc .. –
顯示您的兩個頁面的一些代碼 –