Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textbox Value not saving

Tags:

c#

asp.net

For some reason anything I put into a textbox does not get saved. Could someone please tell me why this is happening? Here is the simplified code I am using:

.aspx

<body>
    <form id="form1" runat="server">
    <div>
    Try this:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>

.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    TextBox1.Text = "Test 1";
}

protected void Button1_Click(object sender, EventArgs e)
{
     Response.Write(TextBox1.Text);
}

The Response.Write always comes out to be "Test 1" no matter what I put in it. Thanks for your help!

like image 712
EverTheLearner Avatar asked Dec 03 '22 15:12

EverTheLearner


1 Answers

You need to check for Post Back in your Page_Load Event before setting. Otherwise, the Page_Load will always overwrite whatever you enter in the textbox.

protected void Page_Load(object sender, EventArgs e)
{
   if(!IsPostBack)
   {
    TextBox1.Text = "Test 1";
   }
}
like image 113
Jose Basilio Avatar answered Dec 24 '22 15:12

Jose Basilio