1. In the embedded code block, the syntax <% = expression %> is used to parse the expression and return its value to the block. Current server time is <% = gettime () %>.
2. embedded code blocks are the server code executed during page rendering. <% For (INT I = 0; I <6; I ++) %>
3. All data binding expressions must be <% # And %> characters.
<asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>
Set: <asp: listboxid = "list" datasource = '<% # myarray %> 'runat = "server">
Expression: <% # expression %> Method Result: <% # getbalance (custid) %>
Add the code to ASP. the default model in the net web page either creates a code hiding class file (Code hiding page ), either write the Page code to the script block with the runat = "server" attribute (single file page ). The written code usually interacts with the control on the page. For example, you can set the text (or other) attribute 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 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 (INT I = 0; I <6; I ++) %> <% {response. write ("<br>" + I. tostring () ;}%> </form> </body> The following code example demonstrates an embedded code block that displays the value of the common gettime () function in the span element. In the embedded code block, the syntax <% = expression %> is used to parse the expression and return its value to the block.
<% @ Page Language = "C #" %> <SCRIPT runat = Server> protected string gettime () {return datetime. now. tostring ("T ");} </SCRIPT> <HTML> <body> <Form ID = "form1" runat = "server"> current server time is <% = gettime () %>. </form> </body> 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 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 <% # And %> characters.
ASP. NET supports the hierarchical data binding model, which creates the 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.
The data binding expression binds data to the control using the eval and bind methods, and submits the changes back to the database. The eval method is a static (read-only) method that uses the value of a data field as a parameter and returns it as a string. The bind method supports the read/write function. You can retrieve the value of the data-bound control and submit any changes to the database.
You can use the XPath and xpathselect methods and the xpathbinder class to bind the xmldatasource control to XML data. For more information, see xmldatasource Web Server 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.0 usage:
<% # Databinder. eval (container. dataitem, "fieldname", "{0: c}") %>
Databinder. Eval parameter 1-data item naming container 2-data field name 3-Format String
ASP. net2.0 usage: <% # eval ("fieldname", "{0: c}") %>
Binding an XML data source <% # XPath ("fieldname") %>
<% # XPath ("shipaddress/address1") %>
Note: If the data-bound variables are defined in the CS code file, they must be declared as public variables. Otherwise, they cannot be used in the binding expression. finally, you must call his on the page. databind () makes binding take effect