ASP. NET inline code, inline expressions, data-binding expressions use a list of methods (form is often said angle brackets percent equals sign)

Source: Internet
Author: User

Today in the Weinan computer Repair Network A small function encountered some problems, so specifically listed for future use.

First, the concept of ASP, inline expressions, and data binding expressions are listed, with detailed concepts and basic usage I'm not going to be here. Please refer to the MSDN details below for a list:

1, <% inline code%>: Inline codes

2, <%=inline expression%>: inline expressions

3, <%# data-binding expression%>: data-binding expressions

Inline code I rarely use, so there is no experience to share with you, inline expression, data binding expression is a necessary tool in my daily work.

The garden in an article written well, here to recommend to everyone: "ASP." NET foreground code binding background Variable method summary

Basic use case presentation:

 //  backstage code  public  string   name;  protected  void  Page_Load (object   sender, EventArgs e) {page.databind ();     //  The foreground code can get the value of name either inline or data-bound expressions  <%= name%><%# name%>//  
Look at the code below, do you see what the problem is?
<Asp:repeaterID= "Repeater1"runat= "Server">     <ItemTemplate>          <Asp:hyperlinkID= "LinkID"runat= "Server"CssClass= "<% #Eval ("CSS ")%>"NAVIGATEURL=' Javascript:methodname (<%# Eval("ID") %>) ' >[concern]</Asp:hyperlink>    </ItemTemplate> 
</Asp:repeater>

Correct wording:

<Asp:repeaterID= "Repeater1"runat= "Server">     <ItemTemplate>//The server tag is not in the correct format. property uses double quotation marks to call the data-binding expression error, instead of single quotes to solve the problem
    <Asp:hyperlinkID= "LinkID"runat= "Server"CssClass= ' <% #Eval ("CSS")%>'NAVIGATEURL='<%# "javascript:MethodName("+Eval("ID")+");"%>' >[concern]</Asp:hyperlink> //For dynamic parameter assignment of the NavigateUrl property to the executed JavaScript method, the writing of the front error does not get its actual value at all, the actual output is actually "javascript:methodname" ( <%# Eval ("ID")%>) "Unlike the results we want, we learned the essence of the foreigner through the ASP. NET HyperLink Eval in a Javascript function, using disguised thinking, we use the data binding expression directly to the concatenation of strings, This gives us the desired effect.
     </ItemTemplate>
</ Asp:repeater >
Inline Expressions <%= The difference between%> and data-binding expressions <%#%>. 1. In background code, the only difference between a data-binding expression and an inline expression is the need to call the DataBind method. Only the DataBind method of the corresponding control is executed, and the bindings in the foreground code that use <%#%> will occur (and all bindings inside the control will also occur, such as nesting a control that binds the background data), otherwise the word will not be assigned, but the default null value. Above we are using the page DataBind method, then the entire page all bindings will execute. Of course, if we only execute the DataBind method of DataList1 or DropDownList1, then only the binding of the corresponding control will occur. It is important to note that what is said here needs to be done DataBind includes display and implicit execution, and some data-bound class controls, when they are bound to a data source control through the DataSourceID property, are implicitly called DataBind methods to perform the binding. It is not necessary to display the call again.

2, the two binding methods, their constraints are basically the same, are required to match the attributes, appear where they can appear. The latter is more widely used, especially in support of server-side controls and binding data collections. Background code, the latter need to call DataBind to complete the binding, the former does not have this requirement. Here are the main differences between the two in the execution mechanism of the difference:<%=...%> is called when the program executes (should be done in the RenderControl event of the page, that is, we can usually see the background code is executed and then go to the foreground code into the <%# ...%> is called after the DataBind () method, and once DataBind () is called, the corresponding control binds the variable, so note that if you modify the variable after DataBind (), the binding is not the most recent value. , it is necessary to DataBind () after the assignment of the variable is completed. In fact, in both ways, it is possible to run the process in VS by setting breakpoints to see when the binding assignments of the two occur.

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.