繁体   English   中英

从aspx访问Session变量到UserControl

[英]access Session variable from aspx into UserControl

我的aspx.cs文件中有一个Session [“ Name”],如何从同一页面上的UserControl代码访问它

谢谢,

尝试这样:

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

要么

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

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

没有看到一点点代码,就很难分辨。

  • 会话可能会超时
  • 是否有错误提示导致会话状态被清除或注销
  • 修改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
}

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

也尝试一下:

HttpContext.Current.Session["Name"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM