我的aspx.cs文件中有一个Session [" Name"] 如何从同一页面上的UserControl代码隐藏中访问它
谢谢,
答案 0 :(得分:0)
尝试这样:
string name= HttpContext.Current.Session["Name"].ToString();
或
string name=Session["Name"].ToString();
答案 1 :(得分:0)
您需要从该控件创建一个对象。
答案 2 :(得分:0)
如果没有看到一点代码,就很难说清楚。
最后我要问: - 会话以某种方式被禁用吗?它在其他页面之间有效吗?也许它被禁用了Ajax请求? - 可能是一个神奇的字符串错误。
就个人而言,为避免出现魔术字符串错误,我使用了Session Wrapper:
/// <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
}
我的示例使用反射将键名定义为与属性名相同,但您可以使用常量。它还会检查会话是否被禁用,这可能有助于调试您的案例。
答案 3 :(得分:-1)
试试这个:
HttpContext.Current.Session["Name"]