Asp. NET: Understanding the Code Model for Web Forms

Source: Internet
Author: User
Tags insert interface modify net visual studio
Asp.net|web a asp.net page consists of two parts: one is the user interface definition of static text and server control, and the other is the implementation of Web application logic in the form of user interface behavior and server-side code.

ASP. NET provides a new code model that enables Web developers and development tools to distinguish code from presentation more clearly and easily. Compared with ASP, this feature is an important improvement, the ASP needs the code throughout the static content of the entire page. Asp. NET code model makes the division of labor in the development and design team easier, and increases the readability and maintainability of code and content.

This new code model usually uses one of two forms. The first form is simply to embed code in the < script runat= "Server" ></script> script block within the. aspx paging file, which is called inline code (inline codes), ASP.net Web Matrix, DREAMWEAVERMX and other software is the code to write the model. The second form contains the implementation of a class derived from the page, saving the code in a separate file and linking it to the. aspx file by using the page instruction. This form is commonly referred to as Code separation (Code-behind) and is sometimes a code-behind technique that is used by Visual Studio.NET. Regardless of the programming model, the performance is the same, just different programming tools, and different writing methods and usage habits, it is important to use the ASP.net class, you must introduce the namespace in the namespaces.

Also, the return processing of Web Forms is based on event-driven, submitting pages as themselves, so the control layout code for each Web form must be placed in <body><form runat=server></from></ Body>html in the code block.

In the following two sections, we illustrate the inline code model and the discrete model that is implemented in visual studio.net2003 through an instance.

  4.2.1 The use of inline code models to write Web applications

The inline code programming model is more approximate to the ASP's upgrade, and the HTML code is saved with the application's logical code in the. aspx paging file, compiled into the page base class the first time it is accessed, and the page class generates the Web page directly for each subsequent visit. In the 5th section of the first chapter, we cite a display "hello,asp.net! "Example, is the use of inline code programming model written. All the code below newfile.aspx:

<%@ Page language= "VB"%>
<! --The Logical Code section begins->
<script runat= "Server" >
' Insert page code here
Sub Page_Load (sender As Object, E as EventArgs)
Response.Write ("Hello, asp.net!")
End Sub
</script>
<! --Logical code partial end->
<body>
<form runat= "Server" >
<!--Insert content here-->
</FORM>
</BODY>
</HTML>

The browser browsing effect is shown in Figure 4.3:


Figure 4.3 newfile.aspx Run results

The benefits of inline code are simpler than with Visual Studio.NET, and each asp.net page in a Web application can be written in a different language, such as newfile.aspx can use vb.net, Newfile2.aspx can be c#,newfile3.aspx using J #. However, each asp.net page must use only one language.

  4.2.2 using code-behind technology to write Web applications

Visual Studio.NET is a typical tool software that uses code-behind techniques to write Web applications. NET provides three different windows for each Web form in a Web application:

(1) Design window: In the WYSIWYG way, you can use the mouse to directly interfere with the control or other visual effects of the location. The toggle button icon for the window is;

(2) HTML code window: You can view the HTML code for a Web form, and you can modify and write it. The Visual Studio.NET system provides the ability to provide smart hints when writing HTML code. Window toggle button icon;

(3) Logical Code window: that is, code-behind technology in the Logical Code window, each Web form has a corresponding logical code file, there are vs.net automatically refer to the Web Form's logical code source to the. aspx paging file. The name of each logical code file is appended to the corresponding asp.net page file name followed by the suffix name. vb (the source file in the C # language is appended with the suffix name. cs). For example, WebForm1.aspx's logical code file is WebForm1.aspx.vb. If you want to enter the logical code for a Web form, simply double-click the Web Forms interface in the design window to enter the logical code window.

We use vs.net2003 to open the Web project ――myfirst application that was built in chapter three, and when opened, the system defaults into the WebForm1.aspx design window, as shown in Figure 4.4.


Figure 4.4 WebForm1.aspx Design window

Click the button icon to enter the WebForm1.aspx HTML code window, as shown in Figure 4.5.


Figure 4.5 WebForm1.aspx HTML Code window

Here is all the HTML code for WebForm1.aspx:

<%@ Page language= "vb" autoeventwireup= "false" codebehind= "WebForm1.aspx.vb" inherits= " Myfirst.webform1 "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
     ; <title>webform1</title>
  <meta name= "generator content=" Microsoft Visual Studio. NET 7.1 ""
  <meta name= "Code_language" content= "visual Basic. NET 7.1"
  <meta Name= "vs_defaultClientScript" content= "JavaScript" "
  <meta name=" vs_targetschema "content=" Http://schemas.microsoft.com/intellisense/ie5 "
   <body ms_positioning=" GridLayout ""
  <form id= "Form1" method= "POST" runat= "Server"
   <font Face= "Song Body" ></font>
  </form>
 </body>

In the <%@ Page language= "vb" autoeventwireup= "false" codebehind= "WebForm1.aspx.vb" inherits= "Myfirst.webform1"%>, Declares the programming language used by the page class, codebehind= "WebForm1.aspx.vb" to indicate that the logical code for the page is saved in the WebForm1.aspx.vb file.

In the WebForm1.aspx Design window, we double-click the form interface to enter the WebForm1.aspx Logical Code window, namely: WebForm1.aspx.vb. WebForm1.aspx.vb is automatically generated by the system, including in its code the declarations that refer to the control in WebForm1.aspx and the code generated automatically by the form designer. When you write code in the WebForm1.aspx.vb window, the system provides intelligent padding, for example, if you don't know the name of the class and only the namespace where the class is, you just knock out the namespace and ".", and the smart fill gives all the class names in that namespace, This is helpful for beginners, and it is also helpful to improve your alias efficiency. The WebForm1.aspx.vb window is shown in Figure 4.6.


Figure 4.6 WebForm1.aspx Logical Code window

In WebForm1.aspx.vb, you include some functions that you must have when you compile your Web forms. The following are all the code for the WebForm1.aspx.vb file:

Public Class WebForm1
Inherits System.Web.UI.Page
#Region the code generated by the Web Forms Designer
' This call is required by the Web Forms Designer.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()
End Sub
' NOTE: The following placeholder declarations are required by the Web Forms Designer.
' Do not delete or move it.
Private Designerplaceholderdeclaration as System.Object
Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mybase.init
' CodeGen: This method call is required by the Web Forms Designer
' Do not modify it using the Code Editor.
InitializeComponent ()
End Sub
#End Region
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
End Sub
End Class

The annotation section in the code above is also automatically added by the system, and the system automatically generates the Page_Load event procedure for the WebForm1.aspx form.
We add the code in this event procedure:

Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
' Here's what to show
Response.Write ("I Love china!" I Love the Great wall! ")
End Sub

and press f5,visual. Studio.NET will automatically compile the entire Myfirst application, then call IE browser Open WebForm1.aspx (WebForm1.aspx as the default startup form in this instance), or right-click in the WebForm1.aspx design form, and in the pop-up menu, select View in browser allows you to view the WebForm1.aspx-compiled runtime directly in the Visual Studio.NET integrated environment, as shown in Figure 4.7, which is appropriate for previewing a Non-default startup form. Of course, you can compile the entire Web application and then enter the address directly in IE to preview the effect.


Figure 4.7 How to preview a Non-default startup form

Previewing Web Forms in either way must be compiled. Visual Studio.NET stores the compiled page base class in the Bin folder under the application root, and calls the base class directly on each subsequent visit. The class generated in this instance is MyFirst.dll. We compile and view WebForm1.aspx through the browser, as shown in Figure 4.8.

Figure 4.8 WebForm1.aspx Run Results

Web applications written with code-behind technology are logically more clear, on the one hand, the file length of the. aspx page is reduced, on the other hand, after the Web application is officially published to the server, the logical code file is compiled to generate the base class file (DLL file) and can be removed to protect the source code.



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.