ASP.NET Web Forms - HTML forms
This section introduces you to the use of ASP.NET Web forms.
All server controls must appear within a <form> tag, and the <form> tag must contain the runat="server" attribute.
All server controls must appear within a <form> tag, and the <form> tag must contain the runat="server" attribute. The runat="server" attribute indicates that the form must be processed on the server. It also indicates that the controls contained within it can be accessed by server scripts:
<form runat="server">...HTML + server controls</form>
Note: This form is always submitted to its own page. If you specify an action attribute, it is ignored. If you omit the method attribute, it will default to method="post". Also, if you do not specify the name and id attributes, they are automatically assigned by ASP.NET.
Note: An .aspx page can only contain one <form runat="server"> control!
If you choose to view the source code on an .aspx page that contains a form that does not have name, method, action, or id attributes, you will see that ASP.NET adds these attributes to the form, as shown below:
<form name="_ctl0" method="post" action="page.aspx" id="_ctl0">...some code</form>
Forms are usually submitted by clicking a button. The format of the Button server control in ASP.NET is as follows:
<asp:Button id="id" text="label" OnClick="sub" runat="server" />
The id attribute defines a unique name for the button, and the text attribute assigns a label to the button. The onClick event handler specifies a named subroutine to be executed.
In the following example, we declare a Button control in the .aspx file. Clicking a button runs a subroutine that changes the text on the button:
Example