访问会话变量从aspx到UserControl

本文关键字:UserControl aspx 会话 变量 访问 | 更新日期: 2023-09-27 17:54:23

我有一个Session["Name"]在我的aspx.cs文件如何从同一页面上的UserControl代码后面访问它

谢谢,

访问会话变量从aspx到UserControl

试试:

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

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

您需要从该控件创建一个对象

如果不看一点代码,就很难分辨。

  • 可能会话超时
  • 是否有错误导致会话状态被清除,或者注销
  • 修改web。配置或许多文件可能导致应用程序重新启动。

最后我不得不问:-会话以某种方式禁用?它能在其他页面之间工作吗?也许它对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
}

我的示例使用反射将键名定义为与属性名相同,但您可以使用常量代替。它还检查会话是否被禁用,这可能有助于调试您的情况。

试一下:

HttpContext.Current.Session["Name"]