Search code examples
c#asp.netsessiontextboxfindcontrol

Tetxtbox inside LayoutTemplate is not taking the changing text in c#


I have two web pages, I send data from one to another using session. From the first web page I get data using session to the second page and set it to the textboxes inside LayoutTemplate as below,

protected void Page_Load(object sender, EventArgs e)
        {
            instructrid = Int32.Parse(Session["instructorId"].ToString());
            ((TextBox)Login1.FindControl("userName")).Text = Session["firstname"].ToString();
            ((TextBox)Login1.FindControl("password")).Text = Session["surname"].ToString();
            ((TextBox)Login1.FindControl("gender")).Text = Session["gender"].ToString();
            ((TextBox)Login1.FindControl("email")).Text = Session["email"].ToString();
            ((TextBox)Login1.FindControl("style")).Text = Session["style"].ToString();
            ((TextBox)Login1.FindControl("phonenumber")).Text = Session["phonenumber"].ToString();
            ((TextBox)Login1.FindControl("hourlyRate")).Text = Session["hourlyRate"].ToString();
            ((TextBox)Login1.FindControl("availability")).Text = Session["availability"].ToString();
        }

Then in the second page user is able to edit those values in the textboxes if needed. Then I take those changed values and send them to the database to update them. But the problem is that the data is not changing from the session value. I am able to edit the values in the textbox but in the backend it is the same value as it was set from the session. Below is the code to grab the values with the button click.

protected void Button1_Click(object sender, EventArgs e)
        {
            string firstname = ((TextBox)Login1.FindControl("userName")).Text;
            string surname = ((TextBox)Login1.FindControl("password")).Text;
            string gender = ((TextBox)Login1.FindControl("gender")).Text;
            string email = ((TextBox)Login1.FindControl("email")).Text;
            string style = ((TextBox)Login1.FindControl("style")).Text;
            string phonenumber = ((TextBox)Login1.FindControl("phonenumber")).Text;
            string hourlyrate = ((TextBox)Login1.FindControl("hourlyRate")).Text;
            string availability = ((TextBox)Login1.FindControl("availability")).Text;
            DBClass.editInstructor(instructrid, firstname, surname, gender, email, style,phonenumber, hourlyrate, availability);
        }

I can't understand how this happens. Please help


Solution

  • Its because you are setting the value in page load and every time the page posts back the old values are copied from the session onto the text box.i.e when you you click the save button the old values are copied on to the textbox first and then it is being saved to db. just put the page load code inside an if(!IspostBack) and it will work.

        protected void Page_Load(object sender, EventArgs e)
        {
           if(!IsPostBack)
           {  
            instructrid = Int32.Parse(Session["instructorId"].ToString());
            ((TextBox)Login1.FindControl("userName")).Text = Session["firstname"].ToString();
            ((TextBox)Login1.FindControl("password")).Text = Session["surname"].ToString();
            ((TextBox)Login1.FindControl("gender")).Text = Session["gender"].ToString();
            ((TextBox)Login1.FindControl("email")).Text = Session["email"].ToString();
            ((TextBox)Login1.FindControl("style")).Text = Session["style"].ToString();
            ((TextBox)Login1.FindControl("phonenumber")).Text = Session["phonenumber"].ToString();
            ((TextBox)Login1.FindControl("hourlyRate")).Text = Session["hourlyRate"].ToString();
            ((TextBox)Login1.FindControl("availability")).Text = Session["availability"].ToString();
           }
       }