C# 为什么ASP文本框有时显示的文本值与';屏幕上有什么?

C# 为什么ASP文本框有时显示的文本值与';屏幕上有什么?,c#,asp.net,cookies,text,textbox,C#,Asp.net,Cookies,Text,Textbox,我在一个ASP网站上创建了一个“记住我”选项,客户说,即使在他们按“注销”后,他们也希望它在用户字段中显示登录名。只要我不再使用其他登录名,它就可以工作。将值指定给代码隐藏中的文本框后,即使手动键入新值,也会使用旧值 例如: 我键入一个用户/密码(如User1),单击“记住我”并成功登录 我注销并被重定向回登录页面。在page_load事件中,它检测到存储了用户名(User1)的有效cookie,并读取值并设置文本字段 我将登录名更改为其他内容(例如User2),但它无法说出无效的用户/密码。奇

我在一个ASP网站上创建了一个“记住我”选项,客户说,即使在他们按“注销”后,他们也希望它在用户字段中显示登录名。只要我不再使用其他登录名,它就可以工作。将值指定给代码隐藏中的文本框后,即使手动键入新值,也会使用旧值

例如:

  • 我键入一个用户/密码(如User1),单击“记住我”并成功登录
  • 我注销并被重定向回登录页面。在page_load事件中,它检测到存储了用户名(User1)的有效cookie,并读取值并设置文本字段
  • 我将登录名更改为其他内容(例如User2),但它无法说出无效的用户/密码。奇怪
  • 我检查数据,它使用的是旧的文本字段(User1)。我尝试另一个(如User3)并按login。它失败了,当我检查Text属性时,它仍然显示User1,尽管屏幕上显示User3
  • 无论我做什么,一旦我在codebehind文件中设置它,它都不会改变。这几乎就像一旦设置了文本字段,就无法更改它。这是不对的,但我没有很好的解释

    以下是相关代码:

    页面加载:

    protected void Page_Load(object sender, EventArgs e)
    {
    
        if (Request.ServerVariables["AUTH_USER"] != null && !Request.ServerVariables["AUTH_USER"].Equals(""))
        {
            login.Visible = false;
            logout.Visible = true;
    
            btnLogout.Text = "Logout " + Request.ServerVariables["AUTH_USER"];
        }
        else
        {
            login.Visible = true;
            logout.Visible = false;
    
    
            CheckLoginCookie();           
        }
    
    }
    
    设置cookie的代码:

    private void SaveLoginCookie()
        {
            try
            {
    
                Response.Cookies["KYSUSR"].Value = txtLoginUsername.Text.Trim();
                Response.Cookies["KYSUSR"].Expires = DateTime.Today.AddMonths(6);
    
            }
            catch (Exception ex)
            {
                ExceptionHandling.SendErrorReport(this, ex);
            }
        }
    
       private void CheckLoginCookie()
        {
            try
            {            
                if (Request.Browser.Cookies)
                {
                    if (Request.Cookies["KYSUSR"] != null && Request.Cookies["KSYFOR"] != null)
                    {
                        // logged in as remember
                        if (Request.Cookies["KYSFOR"].Value == "R")
                            txtLoginUsername.Text = Request.Cookies["KYSUSR"].Value;
                        // once set here, the Text property never changes regardless of what is entered into it
    
                    }                
                }            
            }
            catch (Exception ex)
            {
                ExceptionHandling.SendErrorReport(this, ex);
            }
        }
    
    加载cookie的代码:

    private void SaveLoginCookie()
        {
            try
            {
    
                Response.Cookies["KYSUSR"].Value = txtLoginUsername.Text.Trim();
                Response.Cookies["KYSUSR"].Expires = DateTime.Today.AddMonths(6);
    
            }
            catch (Exception ex)
            {
                ExceptionHandling.SendErrorReport(this, ex);
            }
        }
    
       private void CheckLoginCookie()
        {
            try
            {            
                if (Request.Browser.Cookies)
                {
                    if (Request.Cookies["KYSUSR"] != null && Request.Cookies["KSYFOR"] != null)
                    {
                        // logged in as remember
                        if (Request.Cookies["KYSFOR"].Value == "R")
                            txtLoginUsername.Text = Request.Cookies["KYSUSR"].Value;
                        // once set here, the Text property never changes regardless of what is entered into it
    
                    }                
                }            
            }
            catch (Exception ex)
            {
                ExceptionHandling.SendErrorReport(this, ex);
            }
        }
    
    进行登录的代码:

    protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                String user = txtLoginUsername.Text.Trim();
    
                if (chkSaveUser.Checked)
                    SaveLoginCookie();
                else
                {
                    // set cookie as expired so browser will clear it
                    Response.Cookies["KYSUSR"].Expires = DateTime.Today.AddDays(-1);                
                }
    
                if (CheckLogin(user, txtLoginPassword.Text))
                {
                    if (chkSaveUser.Checked)
                    {                    
                        FormsAuthentication.SetAuthCookie(user, true);                    
                    }
    
                    FormsAuthentication.RedirectFromLoginPage(txtLoginUsername.Text.Trim(), false);
                }
            }
            catch (Exception ex)
            {
                ExceptionHandling.SendErrorReport(this, ex);            
            }
        }
    

    为什么文本属性不会更改?

    问题在于,您没有检查您是否在
    页面加载
    方法中进行回发,这意味着即使用户在
    txtLoginUsername
    文本框中放入其他内容,它也会被
    CheckLoginCookie
    覆盖

    protected void Page_Load(object sender, EventArgs e)
    {
    
        if (Request.ServerVariables["AUTH_USER"] != null
            && !Request.ServerVariables["AUTH_USER"].Equals(""))
        {
            login.Visible = false;
            logout.Visible = true;
    
            btnLogout.Text = "Logout " + Request.ServerVariables["AUTH_USER"];
        }
        else
        {
            login.Visible = true;
            logout.Visible = false;
    
            // Only check the login cookie if we are not dealing with a form submission.
            if (!Page.IsPostBack)
            {
                CheckLoginCookie();
            }
        }
    }
    

    是的,你完全正确!我想明白了,就要编辑了。谢谢你这么快回答@user1505422-很高兴我能帮上忙!