ASP.NET Web Forms
We already mentioned that, all server controls must appear within <form>
and </form> tags, and the <form> tag must contain the runat="server"
attribute.
<form runat="server">
...HTML + server controls
</form>
Please note: Not like classic ASP or PHP, you can add an action attribute
to the form, ASP.NET web form is always submitted to the page itself. If you
specify an action attribute, it is alwarys ignored. If you omit the method attribute,
it will be set to method="post" by default. Also, if you do not specify
the name and id attributes, they are automatically assigned by ASP.NET.
A form usually must be submitted by clicking on a button. The following is
Button server control in ASP .NET:
<asp:Button id="id" text="label" OnClick="sub"
runat="server" />
The id attribute gives 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 execute.
ASP.NET has a default setting for web forms - Maintaining the ViewState. What
is Maintaining the ViewState? Let's say, you have entered a form with a lot
of information. But after click the submit button, the server comes back with
an error. If the form is submitted in classic ASP, you will have to go back
to the form and reenter all the information again because all form values are
cleared when you submit it. But if you use ASP.NET web form, you will save a
lot of coding because ASP .NET maintains your ViewState. The ViewState indicates
the status of the page when submitted to the server.
Please note: Maintaining the ViewState is the default setting for ASP.NET
Web Forms. If you do not want to maintain the ViewState, you must include the
directive <%@ Page EnableViewState="false" %> at the top of
an .aspx page or add the attribute EnableViewState="false" to any
control.
See the following example:
<script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Hello " & txt1.Text & ", Welcome to iTechCollege.com!"
End Sub
</script>
<html>
<body>
<form runat="server">
Your Name: <asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server"
/>
<p><asp:Label id="lbl1" runat="server" /></p>
</form>
</body>
</html>
|