The ASP.NET Web Forms page framework component is a scalable common language runtime programming model that can be used to dynamically generate Web pages. The ASP.NET Page Framework component is a logical evolution of ASP (the syntax provided by ASP.NET is compatible with existing pages), and it was specifically designed to address a number of critical deficiencies in the earlier model. In particular, it provides the ability to create and use reusable UI controls that encapsulate common functionality, thereby reducing the amount of code that page developers need to write; it enables developers to be clear and organized It constructs page logic (not "tangled code"); the development tools it uses provide powerful WYSIWYG (what you see is what you get) page design support capabilities (the existing traditional ASP code is opaque). This article introduces some basic knowledge of ASP.NET page features.
Writing your first ASP.NET page
ASP.NET pages are text files with an .aspx extension. Pages are composed of code and tags that are dynamically compiled and executed on the server to generate display content for the requesting client browser (or device). They can be deployed under the IIS virtual directory tree. When the browser client requests an .aspx resource, the ASP.NET runtime analyzes the target file and compiles it into a .NET framework class. This class can then be used to dynamically handle the request ( Please note that the .aspx file is only compiled the first time it is accessed, and the compiled type instance can be reused for multiple requests in the future).
We don't need to modify the code in the HTML file. We only need to change its extension to .aspx to create an ASP.NET page. For example, the following example demonstrates a simple HTML page that collects the user's name and category, and sends the form back to the original page when the user clicks a button:
<form action="intro1_vb.aspx" method= "post">
<h3> Name: <input id="Name" type=text>
Category: <select id="Category" size=1>
<option>psychology</option>
<option>business</option>
<option>popular_comp</option>
</select>
<input type=submit value="Lookup">
</h3>
</form>
Please note: nothing happens when you click the "Lookup" button. This is because the .aspx file only contains static HTML (no dynamic content). Therefore, the same HTML is sent back to the client, causing the form field (textbox and dropdown list) content to be lost.
Add simple code to the page
The syntax provided by ASP.NET is compatible with the ASP page. It supports <% %> code rendering (render) blocks, which can be mixed with HTML content in .aspx files. These code blocks will be strictly executed when the page is displayed.
The following example demonstrates how to use <% %> rendering blocks (increasing the font size each time) in HTML:
<form action="intro2_vb.aspx" method="post">
<h3> Name: <input id="Name" type=text>
Category: <select id="Category" size=1>
<option>psychology</option>
<option>business</option>
<option>popular_comp</option>
</select>
</h3>
<input type=submit value="Lookup">
<p>
<% Dim I As Integer
For I = 0 to 7 %>
<font size="<%=I%>"> Welcome to ASP.NET </font> <br>
<%Next %>
</form>
Please note: The <% %> code block in the above example is different from ASP in that it will actually be compiled - not interpreted by the script engine. This improves runtime execution performance.
ASP.NET page developers can use <% %> code blocks to dynamically modify HTML output information. For example, the following code demonstrates how to use the <% %> code block to interpret the results sent back by the client:
<form action="intro3_vb.aspx">
<h3> Name: <input name="Name" type=text value="<%=HttpUtility.HtmlEncode(Request.QueryString("Name"))%>">
Category: <select name="Category" size=1>
<%
Dim I As Integer
Dim Values(2) As String
Values(0) = "psychology"
Values(1) = "business"
Values(2) = "popular_comp"
For I = 0 To Values.Length - 1
%>
<% If (Request.QueryString("Category") = Values(i)) %>
<option selected>
<% Else %>
<option>
<%End If %>
<%=Values(i)%>
</option>
<%Next %>
</select>
</h3>
<input type=submit name="Lookup" value="Lookup">
<p>
<% If (Not Request.QueryString("Lookup") = Nothing) %>
Hi <%=HttpUtility.HtmlEncode(Request.QueryString("Name")) %>, you selected: <%=HttpUtility.HtmlEncode(Request.QueryString("Category")) %>
<%End If %>
</form>
Please note: Although the <% %> code blocks provide a powerful way for us to customize the text output information returned by ASP.NET pages, they do not provide a clear HTML programming model. The above example shows that developers who only use the <% %> code block must manage the state of the page and intercept the values being sent by themselves.