Text field value not saved

For some reason, everything that I put in the text box is not saved. Can someone please tell me why this is happening? Here is the simplified code that I use:

.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); } 

Response.Write always acts like "Test 1" no matter what I put into it. Thank you for your help!

+4
source share
1 answer

Before setting up, you need to check Post Back in your Page_Load Event. Otherwise, the_Load page will always overwrite everything that you enter in the text box.

 protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { TextBox1.Text = "Test 1"; } } 
+17
source

Source: https://habr.com/ru/post/1285878/


All Articles