Create a simple web Form with asp.net

Source: Internet
Author: User
Tags count memory usage
Asp. NET introduces a new method of Web programming. This approach is familiar to people who use traditional programming languages such as Visual Basic or C + +. If you're a web programmer who only touches scripting languages, don't worry, this article will get you started quickly.
As programmers, we don't have to mix HTML and code any more, and we don't have to write code from the top down line. Asp. NET makes code and performance separate, using event-driven programming patterns. Here, we'll see the basic structure of the Web form in asp.net.
Asp. NET pages are event-driven and object-oriented. That is, programmers can provide code for events, such as button clicks, page calls, and so on. Each tag that can be accessed programmatically in a page has a runat=server attribute. Although standard HTML controls can still be used, ASP.net provides a more powerful server-side control. These controls provide a set of their own methods and properties that enable them to fully customize the output of the control. These controls are compiled with the page, and they output different HTML depending on the version of the client browser. This allows us to not consider browser-compatible issues, a page can be run on any browser.
We build a page, two input boxes, and collect the visitor's name and favorite color. In addition, we want the result to be returned to itself, and then to display a line of information at the top and maintain the contents of the input box.

This is the traditional ASP page


<%@ Language=vbscript%>
<title>2000081402</title>
<body>
<form id= "Sample1" method= "post" action= "sample1.asp" >
<%
If Request.Form.Count <> 0 Then
Response.Write "Your name is"
Response.Write Request.Form ("Txtname")
Response.Write ", and your FavoriteColor is"
Response.Write Request.Form ("Selcolor")
Response.Write "<br>"
End If
%>
<table cellspacing=0 cellpadding=4 border=0>
<tr>
<td><p Align=right>what is yourname:</p></td>
<td><input type= "text" Name= txtnamevalue= "<%=request.form (" txtname ")%>" ></td></tr>
<tr>
<td><p Align=right>what is Yourfavorite color:</p></td>
<td>
<select name=selcolor>
<option <%if Request.Form ("selcolor") = "Black" Then Response.Write "selected"%>>black</option>
<option <%if Request.Form ("selcolor") = "Blue" Then Response.Write "selected"%>>blue</option>
<option <%if Request.Form ("selcolor") = "Green" Then Response.Write "selected"%>>green</option>
<option <%if Request.Form ("selcolor") = "Pink" Then Response.Write "selected"%>>pink</option>
<option <%if Request.Form ("selcolor") = "Red" Then Response.Write "selected"%>>red</option>
</select>
</td></tr>
<tr>
<td> </td>
<td><input type= "Submit" Id=submit value= "Submit" ></td></tr>
</table>
</form>
</body>

We see here we have to mix ASP code and HTML so that the code is very difficult to read, imagine if a very complex page ...
Our page first determines whether it is a postback or a first visit. We check the Request.Form collection. If it is 0, it is the first visit, otherwise the button for the submission is pressed and we will output a message to the user via Response.Write.
<%
If Request.Form.Count <> 0 Then
Response.Write "Your name is"
Response.Write Request.Form ("Txtname")
Response.Write ", and your FavoriteColor is"
Response.Write Request.Form ("Selcolor")
Response.Write "<br>"
End If
%>

For each element of our form, we have to use code to keep it state. The code is simple and similar.
<td><input type= "text" Name= txtnamevalue= "<%=request.form (" txtname ")%>" ></td></tr>
...
<select name=selcolor>
<option <%if Request.Form ("selcolor") = "Black" Then Response.Write "selected"%>>black</option>
<option <%if Request.Form ("selcolor") = "Blue" Then Response.Write "selected"%>>blue</option>
<option <%if Request.Form ("selcolor") = "Green" Then Response.Write "selected"%>>green</option>
<option <%if Request.Form ("selcolor") = "Pink" Then Response.Write "selected"%>>pink</option>
<option <%if Request.Form ("selcolor") = "Red" Then Response.Write "selected"%>>red</option>
</select>

Asp. One of the benefits of net is simplicity. Common common features no longer need to write a lot of code. Simply declaring a server-side control can provide many features.

This is the page in asp.net.

<%@ Page language= "vb"%>
<title>2000081402</title>
<script language= "VB" runat=server>
Sub Page_Load (Source as Object,e Aseventargs)
If Page.IsPostBack Then
Divresults.innertext = "Your name is" & Txtname.value & _
", and your favorite color is" & Selcolor.value
End If
End Sub
</script>
<body>
<form id= "Sample1" method= "POST" runat=server>
<div id=divresults runat=server/>
<table cellspacing=0 cellpadding=4 border=0>
<tr>
<td>
<p Align=right>what is your name:</p></td>
<td><input type= "text" Id=txtname runat=server/></td></tr>
<tr>
<td>
<p Align=right>what is your favoritecolor:</p></td>
<td>
<select Id=selcolor runat=server>
<option>Black</option>
<option>Blue</option>
<option>Green</option>
<option>Pink</option>
<option>Red</option>
</select>
</td></tr>
<tr>
<td> </td><td>
<input type= "Submit" id=submitvalue= "Submit" runat=server/>
</td></tr>
</table>
</form>
</body>


"Runat=server" Property
Adding a "runat=server" attribute to an ordinary HTML element becomes a ASP.net service-side control. We can access these server-side controls through ID programming. We no longer use Response.Write to output information (although we can still use it), we output information to the user through a DIV tag on the service side.
<div id=divresults runat=server/>

We can programmatically change the properties of the server-side control such as "innertext".

Page_Load event and IsPostBack method
First of all, we want to check if it's not the first visit. Since this is a regular check, ASP. NET provides a way to page: IsPostBack. This method returns True when the page is submitted, and returns false for the first access. We can check in any server-side code. We checked it in the Page_Load incident.
As I said above, each server-side control is an object, including page. As an object, we have access to the object's properties, methods, and events such as Page_Load. When a page is invoked, the event is triggered and the code of the event is executed. If Page.IsPostBack returns True, this is the output information with the InnerText property of the Div control. Exceptions, because the elements in the Form are server-side controls, we can directly visit their properties without using Request.Form. We can use the ID of the control to access the Value property to get the values.
<script language= "VB" runat=server>
Sub Page_Load (Source as Object,e Aseventargs)
If Page.IsPostBack Then
Divresults.innertext = "Your name is" & Txtname.value & _
", and your favorite color is" & Selcolor.value
End If
End Sub
</script>

Management status
Because all of our elements are server-side controls, their state is automatically managed. This state management is done using a hidden field in the page.
The following is the source program in which to browse the Web page.
<title>2000081402</title>
<body>
<form name= "Sample1" method= "post" action= "sample1.aspx" id= "Sample1" >
<input type= "hidden" name= "__viewstate" value= "A0z1019323966_a0z_hz5z2x_a0z_hz5z1x_a0zhzinnerhtml_yourname" is Doug Seven, and your favorite Coloris greenx_xxxxx_x ">
<div id= "Strresults" >yourname is Doug Seven, and your favorite Coloris
<table cellspacing=0 cellpadding=4 border=0>
<tr>
<td>
<p Align=right>what is your name:</p></td>
<td><input value= "Doug Seven" name= "txtname" id= "txtname" type= "text" ></td></tr>
<tr>
<td>
<p Align=right>what is your favoritecolor:</p></td>
<td>
<select name= "Selcolor" id= "Selcolor" >
<option value= "BLACK" >Black</OPTION>
<option value= "Blue" >Blue</OPTION>
<option selected Value= "Green" >Green</OPTION>
<option value= "Pink" >Pink</OPTION>
<option value= "Red" >Red</OPTION>
</SELECT>
</td></tr>
<tr>
<td></td>
<td><input name= "Submit" id= "submit" type= "submit" value= "Submit" ></td></tr>
</table>
</FORM>
</body>

We manage the state in this way, regardless of the server's memory usage, and because the state is in a hidden field, it works well in the Web farm or Web garden.
In this article, we looked at the basic structure of the ASP.net Web form. We can see from the program that the use of server-side controls can reduce the amount of code, easier to control the output.



Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.