Reputation:
Let's say I have a html input box:
<form id="form1" runat="server">
<div>
<input id="Text1" type="text"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
</div>
</form>
If I type something like "My Subjects" into the input, and click the button, the text in the input box will disappear, here is something I don't quite understand:
This is a post request, since "My Subjects" will be submitted to the server, so when the server render the html, it should add the text to the input textbox and then send the html document back to the browser, is my understanding incorrect?
Upvotes: 0
Views: 63
Reputation: 218798
is my understanding incorrect?
No, I'm afraid not. There is no mechanism by which this behavior would be automatic. You would have to write code or use some tool which does this. The ASP.NET Framework has code internally which does this for server controls, which are ones with runat="server"
. (See things like ViewState, and take a look at the hidden fields that ASP.NET automatically adds to your page.)
If you want to populate your page with values included in the request, you would do so in the code for that page. To do this, first you need to give the input a name
so it's included in the POST:
<input id="Text1" type="text" name="Text1"/>
Then you can find the value server-side in the POST request with something like:
Request.Form["Text1"]
From there you can put it on the page in a variety of ways, just like any other value in your class that you want to put on the page.
Upvotes: 1
Reputation: 154995
ASP.NET WebForms only adds the "postback magic" to HTML elements if they have the runat="server"
attribute applied, otherwise they're treated as static plain text.
Add runat="server"
and it will work:
<input id="Text1" type="text" runat="server" />
Upvotes: 1