Analysis of embedded code block application in ASP. NET web pages

Source: Internet
Author: User

What are embedded code blocks in ASP. NET web pages? What is its role? Let's start:

Add the code to ASP. the default model in the NET web page either creates a code hiding class file code hiding page), or writes the Page code to a single file page in the script block with the runat = "server" attribute ). The written code usually interacts with the control on the page. For example, you can set the Text or other properties of the control from the code to display information on the page.

Another possibility is to use embedded code blocks to embed code directly into pages.

Embedded ASP. NET code block

Embedded ASP. NET code block is the server code executed during page rendering. The code in the block can execute programming statements and call functions in the current page class.

The following code example shows an ASP. NET page containing an embedded code block. The code block displays the cyclic result.

Visual Basic

 
 
  1. ﹤%@ Page Language="VB" %﹥  
  2. ﹤html﹥  
  3. ﹤body﹥  
  4.     ﹤form id="form1" runat="server"﹥  
  5.     ﹤% For i As Integer = 0 To 5 %﹥       ﹤% Response.Write("﹤br﹥" & i.ToString())%﹥    ﹤% Next%﹥  
  6.     ﹤/form﹥  
  7. ﹤/body﹥  
  8. ﹤/html﹥C#  
  9. ﹤%@ Page Language="C#" %﹥  
  10. ﹤html﹥  
  11. ﹤body﹥  
  12.     ﹤form id="form1" runat="server"﹥  
  13.     ﹤% for(int i = 0; i ﹤ 6; i++) %﹥       ﹤% { Response.Write("﹤br﹥" + i.ToString()); }%﹥  
  14.     ﹤/form﹥  
  15. ﹤/body﹥  
  16. ﹤/html﹥ 

The following code example demonstrates an embedded code block. The ASP. NET code block displays the value of the common GetTime () function in the span element. In the embedded ASP. NET code block, the syntax <%= expression %> is used to parse the expression and return its value to the block.

Visual Basic

 
 
  1. ﹤%@ Page Language="VB" %﹥  
  2. ﹤script runat=server﹥  
  3. Protected Function GetTime() As String  
  4.     Return DateTime.Now.ToString("t")  
  5. End Function  
  6. ﹤/script﹥  
  7. ﹤html﹥  
  8. ﹤body﹥  
  9.     ﹤form id="form1" runat="server"﹥  
  10.        Current server time is ﹤% =GetTime()%﹥.  
  11.     ﹤/form﹥  
  12. ﹤/body﹥  
  13. ﹤/html﹥C#  
  14. ﹤%@ Page Language="C#" %﹥  
  15. ﹤script runat=server﹥  
  16. protected String GetTime()  
  17. {  
  18.     return DateTime.Now.ToString("t");  
  19. }  
  20. ﹤/script﹥  
  21. ﹤html﹥  
  22. ﹤body﹥  
  23.     ﹤form id="form1" runat="server"﹥  
  24.        Current server time is ﹤% =GetTime()%﹥.  
  25.     ﹤/form﹥  
  26. ﹤/body﹥  
  27. ﹤/html﹥ 

Embedded code blocks must be written in the default language of the page. For example, if the @ Page command of a Page contains the attribute language = "VB ", the page uses the Visual Basic compiler to compile the code in all the script blocks marked with runat = "server" and all the embedded code in the <%> separator.

Use of embedded ASP. NET code blocks

ASP. NET web pages support embedded code blocks, which are mainly used to retain backward compatibility with the old ASP technology. Generally, applying embedded code blocks to complex programming logic is not the best practice, because debugging and maintenance are difficult when code and tags on pages are mixed. In addition, because the code is only executed during page rendering, it is much less flexible than placing the code in the appropriate page processing phase to execute background code or script block code.

Some functions of embedded code blocks include:

Set the value of the control element or tag element to the value returned by the function, as shown in the previous example.

Directly embed computing into tag or control attributes.

The Application of ASP. NET code blocks on Web pages will be introduced here, hoping to help you.

  1. Experience in ASP. NET code separation
  2. Detailed research on ASP. NET data verification technology
  3. Application of ASP. NET code separation in website construction
  4. Analysis on ASP. NET code optimization
  5. Analysis on the use of CustomValidator, an ASP. NET data verification control

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.