【问题标题】:assigning Null to the get set method of the class but does not assign null将 Null 分配给类的 get set 方法但不分配 null
【发布时间】:2015-01-21 17:01:27
【问题描述】:

在登录时,值存储在“Estb_detail”类的方法中,但在注销时,我什至将 null 分配给这些方法,但它们不会为空,并且仍然填充相同的值。

下次登录时,它会再次返回不需要的旧值。

Estb_detail.cs:

public class Estb_detail : System.Web.UI.Page
{
    public Estb_detail()
    {

    }


    private string estbName;

    public string EstbName
    {
        get { return HttpContext.Current.Application.Contents["estbName"] as string; }
        set { HttpContext.Current.Application.Add("estbName", value); }
    }
    private string estbEmail;

    public string EstbEmail
    {
        get { return HttpContext.Current.Application.Contents["estbEmail"] as string; }
        set { HttpContext.Current.Application.Add("estbEmail", value); }
    }
    private string companyId;

    public string CompanyId
    {
        get { return HttpContext.Current.Application.Contents["companyId"] as string; }
        set { HttpContext.Current.Application.Add("companyId", value); }
    }

    private string type_account;

    public string Type_Account
    {
        get { return HttpContext.Current.Application.Contents["type_account"] as string; }
        set { HttpContext.Current.Application.Add("type_account", value); }
    }    
}

登录.aspx.cs:

public partial class Login : Estb_detail
{
 protected void _btnLogin_Click(object sender, EventArgs e)
    {
    using (Estb_cntxtDataContext context = new Estb_cntxtDataContext())
                {
                        var user = (from a in context.tblCustomers where a.Email == _txtName.Text && a.Password == _txtPassword.Text select a).FirstOrDefault();


                        if (user != null)
                        {                    
                            Session["Establishment"] = user.Name;
                            EstbName = user.Name;
                            EstbEmail = user.Email;
                            CompanyId = user.nCompany_Id.ToString();
                            Type_Account = "Master";

                            Response.Redirect("Establishment_Controller.aspx");
                        }
               }
   }
}

Establishment_controller.aspx.cs:

public partial class Establishment_Controller : Estb_detail
{
  protected void _Logout_Click(object sender, EventArgs e)
    {
        Session.Clear();

        EstbName = "";
        EstbEmail = "";
        CompanyId = "";
        Type_Account = "";
        Response.Redirect("Login.aspx");        
    }
}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    使用同一个键多次调用HttpApplicationState.Add() 将添加多个值,而不是覆盖现有值。

    http://codeverge.com/asp.net.state-management/httpapplicationstate-allows-duplicat/307305

    在设置器中,您需要检查值是否已经存在,如果存在,则使用数组访问运算符更改其值。或者,在添加之前使用.Remove() 删除密钥。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 2021-06-08
      • 2021-06-12
      • 2022-01-27
      • 1970-01-01
      • 2023-01-25
      相关资源
      最近更新 更多