SetCodeThe default model added to the ASP. NET webpage either creates a code hiding class file (Code hiding page) or writes the Page code toRunat = "server"AttributeScriptBlock (single file page ). The written code usually interacts with the control on the page. For example, by settingText(Or other) properties. Information can be displayed on the page.
Another possibility is to use embedded code blocks to embed code directly into pages.
Embedded code block
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.
The following code example shows an ASP. NET page containing an embedded code block. The code block displays the cyclic result.
<% @ Page Language ="C #"%>
<HTML> <body>
<Form ID ="Form1"Runat ="Server">
<%For(IntI = 0; I <6; I ++) %>
<% {Response. Write ("<Br>"+ I. tostring () ;}%>
</Form>
</Body>
</Html>
The following code example demonstrates an embedded code block, which is displayedSpanCommon ElementGettime ()The value of the function. In embedded code blocks, the syntax<% = Expression %>It is used to parse the expression and return its value to the block.
<% @ Page Language ="C #"%>
<SCRIPT runat = Server>
ProtectedString gettime ()
{ReturnDatetime. Now. tostring ("T");}
</SCRIPT>
<HTML>
<Body>
<Form ID ="Form1"Runat ="Server"> Current server timeIs<% = Gettime () %>.
</Form>
</Body>
</Html>
Embedded code blocks must be written in the default language of the page. For example, if the @ page command of a page contains attributesLanguage = "VB", The page will use the Visual Basic compilerRunat = "server"All the code in the script block and<%>Compile all embedded code in the separator.
Use of embedded 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.
Another kind of embedded code is data binding.
Data Binding expression syntax
All data binding expressions must be included in<% #And%>Characters.
ASP. NETSupports layered data binding model. This model creates binding between server control attributes and data sources. Almost any server control attribute can be bound to any public field or attribute. These public fields or attributes are located in the direct naming container that contains pages or server controls.
Use the data binding expression Eval And Bind Method to bind data to the control and submit the changes back to the database.Eval The static (read-only) method uses the value of a data field as a parameter and returns it as a string.Bind Read method supported/The Write function retrieves the value of the data-bound control and submits any changes to the database.
Available Xpath And Xpathselect Method andXpathbinderClass slaveXmldatasourceControl boundXMLData. For more information, seeXmldatasource WebServer Control.
<Asp: Label text = '<% # statelist. selecteditem. Text %> 'runat = "server"/>
Set: < ASP : Listboxid = " List " Datasource= ' <% # Myarray%> ' Runat= " Server " >
Expression: <% #Expression %>
Result:<%#Getbalance (custid)%>
Databinder. eval ()Method:
ASP. net1.0Usage:
<% # databinder . eval (container. dataitem, " fieldname " , " {0: c} " ) %>
Databinder. EvalParameter 1-data item naming container 2-data field name 3-Format String
ASP. net2.0 Usage: <% # Eval ( " Fieldname " , " {0: c} " )%>
XMLData Source binding<%#XPath ("Fieldname")%>
<% # XPath ("Shipaddress/address1") %>
Note: variables bound to data must be defined in the CS code file.PublicStatementIt is a public variable, otherwise it cannot be used in the binding expression. Finally, it must be on the pageCallHis. databind ()Make binding take effect