In fact <%%> has seen it before, will be a small feature only. Others do not understand. Today, occasionally, we see its true colors, today, to share.
Grammar
code block Rendering (<%%>) defines inline code or inline expressions that run when a page is rendered, with detailed syntax such as the following:
<%code%>//Inline Code
<%=expression%>//Inline expression
You can define separate lines or blocks of code using inline code.
It is the server code that runs during the rendering of the page.
A description of the use of embedded code blocks, such as the following:ASP. NET Web pages support embedded blocks of code. Used primarily to preserve backward compatibility with legacy ASP technologies.
Under normal circumstances. Using embedded code blocks for complex programming logic is not a best practice because it is difficult to debug and maintain when the code in a page is mixed with markup.
Also, because the code will run during the rendering of the page. As a result, flexibility is greatly reduced compared to the appropriate page processing stage to run code or script block code.
Some uses of embedded code blocks include:
Sets the value of the control element or markup element to the value returned by the function.
Embeds a calculation directly into a tag or control property.
Demo SampleDemo Example 1: Output a string of 5 lines labeled "Information technology accelerated" on the page. Execution Effect
The code is as follows:
<%@ page language= "C #" autoeventwireup= "true" codebehind= "WebForm1.aspx.cs" inherits= "Testlocaldata.webform1"% ><! DOCTYPE html>
Demo Example 2: An inline expression is a shortcut that invokes the Write method. <%=expression%> used to parse an expression. and returns its value to the block.
The following Demo sample code displays the value of the GetTime method by using an inline expression. What you see when performing the effect:
The code is as follows:
<%@ page language= "C #" autoeventwireup= "true" codebehind= "WebForm2.aspx.cs" inherits= "Testlocaldata.webform2"% ><! DOCTYPE html>
Note : The code in the code block must be written using the default language of the page.
Like what. Assuming that the page's @page directive includes the property language= "C #," The page uses the Visual C # compiler to code the entire script block labeled Runat=server. And all the embedded code in the <%%> code block to compile.
Demo Sample 3: Apply <%%> code block to implement simple property binding in ASP.
First understand what data binding is. Data binding not only outputs the data types that the general output statement can output, but also outputs any data source that conforms to the data binding requirements. And for different data display control to the data source processing can be inconsistent, but also can be changed according to the situation changes dynamically. In other words, in data binding, the display of the data is determined by the data source and the data display control together. The data source determines the content of the data, and the data display controls determine how the data is displayed. In fact, the mechanism of data binding is to get the data by means of the data display control invoking the data source.
The syntax for data binding is as follows:
< language tags ... Property ' <% data-binding expression%> ' runat= "Server" >
Suppose you output the result of an expression directly to a Web page. So the syntax for data binding is as follows:
String: <% data-binding expression%>
Demo Sample Execution Results
The following are the main steps to implement the program, which should normally be taken from the database. Here to demonstrate the convenience of binding dead data):
(1) Create a new ASPX page. In the foreground page code, define two public properties that act as the data source when the data is bound, and then apply the <%%> code block to establish a binding relationship between it and the display control. The full demo sample code is as follows:
<%@ page language= "C #" autoeventwireup= "true" codebehind= "WebForm3.aspx.cs" inherits= "TESTLOCALDATA.WEBFORM3"% ><! DOCTYPE html>
Description: attribute-based data binding involves a property that must include a get accessor. Because of the data binding process. Data display controls require properties to read data from properties in the get accessor
(2) After the binding is complete, simply call the page class's DataBind method in the Page_Load event of the pages to actually read the data now when the page is loaded. The code is as follows:
protected void Page_Load (object sender, EventArgs e) { page.databind ();}
When we see a strange knowledge or noun should not just stay in the level of use, but should be "know its why" closer, understand the ins and outs, even if not mastered. At least you have to know. For the often used knowledge of the expansion of the search after the completion of the need for digestion, practice, so that talent to expand their own eyes of the world. And can face the knowledge variant after calmly face.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
<%%> Create an inline code block (Express)