What is the difference between,<%#%>,<%=%> and <%%> in ASP.

Source: Internet
Author: User
Tags connectionstrings

(Reproduced:http://blog.csdn.net/lai123wei/article/details/7261701)
HTML code that contains this form of <%%> is often present in ASP. In general, it contains the following formats:A. <%%>This format is actually the same as the ASP usage, except that the ASP contains VBScript or JavaScript code, whereas in ASP. NET is the language supported under the.        Special Note: The server control cannot have <%%> syntax (here in C # code) <% int a = 2;        int b = 3;        int C = a + B; Response.Write (c);
%>two. <%#%>If this is the format, it is unique to ASP.It is the syntax of the control data binding and must be called by the DataBind () method of the control to execute(or the entire page page.databind () is called the DataBind () method on the resulting control, in which case both the server control and the client control are bound) pay special attention to:only server controls can use <%#%> syntax(inaccurate) when the entire page is called this. DataBinder () method, both the server control and the client control are bound <div>server control:<asp:textbox id= "TextBox1" runat= "Server" text= " <% #text%> "></asp:textbox><br/><!--Server control-->client control:<input type=" text "Id=" TextBox2 "value=" <% #text%> "/><!--Client control--> </div>
The Aspx.cs code is as follows:
Protected string text;//Note that this must be declared as public or protected, otherwise the ASPX page (subclass) cannot access protected void Page_Load (object sender, EventArg S e) {if (! Page.IsPostBack) {this.text = "aaaaaaaaaaaaa"; This. Textbox1.databind ();//or this. DataBind (); }} when this. Textbox1.databind () runs the displayed effect (cannot insert the picture itself test) this. DataBind () Run the displayed effect (unable to insert picture yourself test)three. <%=%>This form is actually extended by <%%>. Equivalent to: Response.Write (<%%>) This form (not standard, but can see), it can actually be seen as a binding. aspx code:
<label id= "Label1" ><%=displaystr ()%></label><br/> <label id= "Label2" runat= "Server" > <%=displaystr ()%></label>

Aspx.cs Code:
public string Displaystr ()//Note that there must be a return value, otherwise a run-time error {return ' bbbb ' will occur; }Four .<%$%>This form is primarily used to bind key-value pairs of Web. config files: strings typically used to connect to a databaseSpecial Note: 1. Bind only to server Controls 2. You can only bind to a property on a server control. <asp:textbox runat= "Server" id= "CC" text= "<% $ConnectionStrings:p ubs%>" ></asp:TextBox>

The Web. config file is as follows:
<connectionStrings> <add name= "pubs" connectionstring= "server=.; Database=pubs;uid=sa;pwd= "providername=" System.Data.SqlClient "/> </connectionStrings> this will work. If you modify a file in aspx:
Use the client's control <input type= "text" value= "<% $ConnectionStrings:p ubs%>"/> or: not bound to a property on a server control <asp:textbox runat= "Server" id= "CC" ><% $ConnectionStrings the same error occurs:p ubs%></asp:textbox> runThe last thing to note:<%#%> is only for server control data binding and therefore cannot be supplemented with <%=%> and <%%> blending: for:<%=%>: Another scenario: aspx code: <input type= "Tex T "id=" a "value=<%=displaystr ()%>/> <input id=" Text1 "type=" text "runat=" Server "VALUE=&LT;%=DISPL Aystr ()%>/>aspx.cs Code: public string Displaystr ()//Note that there must be a return value, otherwise a run-time error {return ' bbbb ' will occur; }
If this is the case, a run-time error occurs. Finally, this <%=%> usage emphasizes: Try to use this form for the client control, after all, it is the ASP common syntax, there is no problem. However, the server controls for ASP. NET may not be fully suitable.

What is the difference between,<%#%>,<%=%> and <%%> in ASP.

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.