Although form.html can generate postbacks via post or Get methods, it still cannot handle user input and is still a static page, and now we need to handle user input and postback on the server side.
There are many ways to handle user input on the server side, because this is the ASP.net tutorial, so just talk about asp.net.
Asp. NET uses Web Forms to describe Web pages, with the. NET Framework in object-oriented programming to make Web application design simpler, more efficient, and more powerful.
2. Web Forms
Let's start with a simple sample program:
Doadd.aspx
<script language= "C #" runat= "Server" >
void Add (object sender, EventArgs e)
{
if (value1. Text = "")
Value1. Text = "0";
if (value2. Text = "")
value2. Text = "0";
Try
{
Result. Text = (int. Parse (value1. Text) + Int. Parse (value2. Text)). ToString ();
}
Catch
{
Result. Text = "Error";
}
}
</script>
This assumes that the reader has properly installed IIS and asp.net, and that the Web server's home directory is C:\Inetpub\wwwroot\ and later, without special instructions.
Place the file in C:\Inetpub\wwwroot\, save as Doadd.aspx, and then enter http://localhost/doadd.aspx in the browser's address bar, and the Web form is rendered in the browser.
There are 2 input boxes on the page, a button, when the user clicks the "=" button, the program adds the values in the 2 input boxes, then displays the result after the equal sign button, and the result displays "error" If the error value is entered.
This program is very simple, but it is a complete Dynamic Web program. It processes user input on the server side, and then dynamically generates HTML pages.
After using IE to open the page, select Menu "View" | "source file" and you will see the following HTML source code:
As you can see, the TextBox control becomes the <input type= "text"/> tag, the button control becomes the <input type= "submit"/> tag, and the label control changes <span> </span> tag, which also has a strange <input type= "hidden" > Label, named __viewstate, Now you just need to know that it's a mechanism used by ASP.net to handle round-trip data between servers and browsers, which will be covered in more detail later.
Viewing an ASPX file, you can see that the form label and all of its controls have a property: runat= "Server", which means that the label runs on the server side rather than directly to the client browser.
A <asp:...> control actually corresponds to a class in a System.Web.UI.WebControls, such as <asp:textbox/> Corresponds to System.web.ui.webcontrols.textbox,<asp:button/> Corresponding to System.Web.UI.WebControls.Button, these tags can also contain other properties, such as <asp:Button/> The Text property corresponds to the Text property in the System.Web.UI.WebControls.Button class, which is a special id attribute that represents the programmable name of the control that is used to access the control in code.
The <script></script> script block is also marked for runat= "server" because it is code that runs on the server side, not client script.
Observe the <asp:button/> tag, which has a property: onclick= "Add", which specifies an event handler that triggers the Click event when the user clicks the button, and the program calls <script></ The Add method in the Script> block. The rule for adding event handling is to prefix the event name "on" as a property of the control's markup. For example: <asp:button onload= "Button_load" runat= "Server"/>, where the event names can be consulted for public events in the corresponding classes in the. NET Framework SDK documentation.
Here is a brief description of the underlying principle of the event model, in the server returned to the client in HTML, there is an HTML form, which has a Submit button, click the button, the browser use the Post method to the <input> control input back to the server, ASP. NET knows that the user clicked the "=" button and generated a postback that responds to the Click event on the server side, so asp.net calls the Add method and then builds the HTML page again because the Text property of the label control is assigned a value when the Add method is called, so the resulting HTML The <span></span> tag contains a string that corresponds to the value of the Text property of the label control.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.