将Null指定给类的get set方法但不指定null

时间:2015-01-21 08:51:04

标签: c# asp.net

在登录时,值存储在类“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); }
    }    
}

Login.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");        
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

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