Use of angle brackets and percent signs in asp.net code

Source: Internet
Author: User

1. The variable name in <% = %>, such:
<Div>
<H1> Hello World <P> Welcome to Beginning ASP. NET 3.5 on <% =
DateTime. Now. ToString () %> </p>
</Div>
Output result:
Hello World
Welcome to Beginning ASP. NET 3.5 on 15:53:08
 
2. <% # %> Data Binding
Generally used in asp containers such as asp: repeater and datalist, Bind containers and data sources through such as repeater. datasource = DataSet; repeater. Bind.
For example: <% # DataBinder. (Container. DataItem, "ClassName") %>
Complete code: <asp: DataList ID = "ClassList" runat = "server">
<ItemTemplate> <% # DataBinder. (Container. DataItem, "ClassName") %>
</ItemTemplate>
</Asp: DataList> </td>
 
3. <% @ %> indicates reference.
For example, you can see the following code on many. aspx pages:
<% @ Page Language = "C #" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
 
4. <%: %>
This method is a newly added binding method in ASP. NET4.0 and is often used in MVC, but can also be used in common webform. Its function is to encode the bound value. Therefore, the two are equivalent:
<% = Server. HtmlEncode ("<B> test </B>") %>
<%: "<B> test </B>" %>;
 
5. Generally, functions or methods are put in the middle, and typical asp programming method is used.
In the percent sign, if the percent sign is not followed by any symbol (colon, equal sign, or well number), it indicates that you want to execute a piece of code, which does not contain any output information. If the percent sign is followed, that is, the code is executed here, and the value returned after execution is bound (or displayed) here.
Example: <tr bgcolor = "# ffffff">
<Td height = "20">
<Div align = "center"> category: </div>
</Td>
<Td height = "9"> & nbsp;
<% ST_getList (); %>
</Td>
</Tr>
 
6. There are some differences between <% = %> and <% # %>.
In the background code, the only difference from the first method is that you need to call the DataBind method. Only when the DataBind method of the corresponding control is executed will binding <% # %> be used in these controls in the front-end code (and all bindings inside the control will also occur, for example, if another control is nested to bind background data), otherwise it will not be assigned a value, but will be null by default. We used the DataBind method of the Page above, so all bindings to the whole Page will be executed. Of course, if we only execute the DataBind method of DataList1 or DropDownList1, only binding the corresponding control will happen. Note that the execution of DataBind includes display and implicit execution. Some data binding controls are used when they are bound to the data source control through the performanceid attribute, the binding is executed by calling the DataBind method implicitly. In this case, you do not need to display the call again.
 
Differences:
In the two binding methods, their constraints are basically the same, both of which must match the attributes and appear where they can appear. The latter is widely used, especially for server-side controls and binding data sets. In terms of background code, the latter needs to call DataBind to complete binding. The former does not have this requirement. The difference between the two is as follows: <% =... %> is called during program execution (it should be completed during the RenderControl event on the page, that is, the background code we can see is usually executed and then assigned to the foreground code), and <% #... %> is called after the DataBind () method. Once DataBind () is called, its corresponding control is bound to a variable. Therefore, note that if () then modify the variable, so the bound value is not the latest value. This requires you to complete the variable assignment and then go to DataBind (). In fact, the running process of these two methods can be viewed in VS by setting breakpoints to see when the binding values of the two methods occur.


From Xiao shenke's salvation. com

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.