Asp.net programming Asp.net code writing method (post code and embedded code)

Source: Internet
Author: User
Asp.net 2.0 is used to construct Code There are two ways.

The first is the background encoding mode.
There are two ways to write,
Method 1: write code in. aspx. CS. The advantage of this is that the Code is separated from the page content to make the code clearer.
Method 2: write code in the script block (single file page) with runat = "server" attribute in Aspx.

For example:

Below<ScriptRunat= "Server">Angular Block And"Cs files in the background"All are in the background encoding mode.

The code of the backend parameter encoding mode is explained by the script engine.

The second is embedded code mode.

This method is similar to the old style ASP page. It writes the code between <%> In Aspx.

Example:Code

<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Default. aspx. CS " Inherits = " _ Default "   %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head Runat = "Server" >
< Title > No title page </ Title >

<% -- Post Code -- %>
< Script Runat = "Server" >
String sval =   " Valtest " ;
</ Script >

</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< ASP: button ID = "Btntest" Runat = "Server" Text = "Btntest"   />
< Div >
<% --Embedded Code -- %>
<% For ( Int I =   0 ; I <   10 ; I ++ )
{
Response. Write ( " <Br/> "   + I. tostring ());
}
%>
< BR />
<% -- Call the gettime () method in the background -- %>
Current time is <% = Gettime () %>
< BR />
<% --Call the background code in the script section. -- %>
<% = Sval %>
</ Div >
</ Form >
</ Body >
</ Html >

Backend CS files:

Code

Public   Partial   Class _ Default: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{

}
///   <Summary>
/// Get time
///   </Summary>
///   <Returns> </returns>
Public   String Gettime ()
{
String Stime =   "" ;
Return Stime = Datetime. Now. tostring ();

}
}

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

This Code: The <%> code block is different from ASP. It will be truly compiled-rather than interpreted by the script engine, that is, the code is compiled, not interpreted, the performance will be much better.

Comparison:
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,
Directly embed computing into tag or control attributes.

 

Note: Part of the content is taken from msdn

 

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.