简体   繁体   English

从aspx访问Session变量到UserControl

[英]access Session variable from aspx into UserControl

I have a Session["Name"] in my aspx.cs file how to access it from the UserControl codebehind on the same page 我的aspx.cs文件中有一个Session [“ Name”],如何从同一页面上的UserControl代码访问它

Thanks, 谢谢,

try like this: 尝试这样:

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

or 要么

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

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

Without see a little bit of the code, it would be hard to tell. 没有看到一点点代码,就很难分辨。

  • Maybe the session is timing out 会话可能会超时
  • Is there an error happing that causes the session state to be clear, or an logout 是否有错误提示导致会话状态被清除或注销
  • Modifying the web.config or many files can cause the application to restart. 修改web.config或许多文件可能会导致应用程序重新启动。

Finally I have to ask: - Is session disabled in some way? 最后,我不得不问:-是否以某种方式禁用了会话? Does it work between other pages? 是否可以在其他页面之间使用? Maybe it is disabled for Ajax requests? 也许对Ajax请求禁用了吗? - Is is maybe a magic string error. -可能是魔术字符串错误。

Personally, to avoid magic string errors, I use a 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
}

My example uses reflection to define the key name as the same as the property name, but you can use constants instead. 我的示例使用反射将键名定义为与属性名相同,但是可以改用常量。 It also checks if the Session is disabled, which might help in debugging your case. 它还检查会话是否被禁用,这可能有助于调试您的情况。

也尝试一下:

HttpContext.Current.Session["Name"]

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

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