views:

783

answers:

3

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?

+4  A: 

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
David Basarab
but he's doing `username = (string)Request.QueryString["username"];` on every page load.
Kobi
@David Basarab, I am not requesting a solution. I am trying to know the difference of effect btwn. protected vs private in this case.
JMSA
+2  A: 

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.

Joel Coehoorn
If you redirect to a new page or load a page with a new query string, that is not a postback. If you process a button click event on a page with a query string, that is a postback and the query string is sent with the postback http request. Either way, it's still a new instance of the page class.
Joel Coehoorn
I did not find my answer relating protected vs private.
JMSA
+1  A: 

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?

in addition, a protected field will be visible on the page (TeacherControlPanel.aspx, on <% %> statements), while a private field isn't.
Kobi