ASP.NET private member field loses value on postback

醉酒当歌 提交于 2019-12-10 03:02:51

问题


Consider the following code:

    public partial class TeacherControlPanel : System.Web.UI.Page
    {
        protected string username = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            username = (string)Request.QueryString["username"];

            Ice_Web_Portal.BO.Teacher teacher = Ice_Web_Portal.BO.Teacher.GetTeacherByUsername(username);

            if (teacher != null)
            {
                labUsername.Text = username;
                labName.Text = teacher.TeacherName;
                labTeacherCode.Text = teacher.TeacherCode;

                Dept dept = teacher.Department;

                if (dept != null)
                {
                    labDepartment.Text = dept.DeptName;
                }
            }
            else
            {
                //labErrorMessage.Text = "No teacher found";
            }
        }

        protected void btnSendMail_Click(object sender, EventArgs e)
        {
            Response.Redirect(@"~/Teacher/TeacherComposeMail.aspx?username=mahabub" + username);            
        }
}

In this code, when I am declaring 'username' as private, it is initialized to null after subsequent post backs.

Why?

What is the secret?


回答1:


Because ASP.NET is stateless meaning it does not keep it state from post back to postback. Save the user to the viewstate, session, or application to see it on postback to postback.

#region UserName
public string UserName
{
    get
    {
        if (this.ViewState["UserName"] == null)
            return string.Empty;

        return (string)this.ViewState["UserName"];
    }
    set { this.ViewState["UserName"] = value; }
}
#endregion



回答2:


Every time you do any postback, even for "simple" things like button click events, you're working with a new instance of the page class. That's ASP.Net 101.




回答3:


Declaring the username field as private or protected has no bearing on this situation. The only bearing protected/private would have is the accessibility of the variable outside the class or in inherited members.

I believe this is likely a lifecycle problem.

When you navigate to this page for the first time, user name will only have a value if the query string was set for the request. So, "/TeacherControlPanel.aspx" will have a user name with no value, but "/TeacherControlPanel.aspx?username=SomeUserName". In these cases, the field username is only going to have a value if one is set. And if no querystring is set, then when the page processes the button click event, the load will fire, no query string set means that username will be null, which means that the click event will have nothing to append to the redirect string.

So the question is, in your application, what navigation path are you using to get to TeacherControlPanel.aspx?



来源:https://stackoverflow.com/questions/1313697/asp-net-private-member-field-loses-value-on-postback

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!