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!
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";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With