Use of <%%> in ASP

Source: Internet
Author: User
Tags connectionstrings

Code separation mode in ASP. We are certainly not unfamiliar, C # (or other languages) written code is generally not mixed with the design language HTML, but sometimes can not be avoided, then in the UI page with <%%> to bind the display, binding variable data, Today I saw an article devoted to this analysis and I made a few corrections:

A. <%%>

This format is actually the same as the ASP usage, but in the ASP is the VBS CRIPT or Javas Cript code, and in ASP. NET is supported by the language.

Special NOTE: The <%%> syntax cannot be included in the server control, otherwise an error occurs.

Below, we can see the following code in the. aspx page:

<%int2; int 3 ; int C = a + b; Response.Write (c); %>

Two. <%#%>

This format is unique to ASP, which is the syntax of the control data binding, and must be called by the DataBind () method of the control (or the entire page page.databind () is called the DataBind () method on the resulting control)

Special Note: Only server controls can use the <%#%> syntax

<div>Server Control:<asp:textbox id="TextBox1"runat="Server"text="<% #text%>"&GT;&LT;/ASP:TEXTBOX&GT;&LT;BR/><!--Server control-->Client Control:<input type="text"Id="TextBox2"Value="<% #text%>"/><!--Client control--></div>


The Aspx.cs code is as follows:

protected stringText//Note that this must be declared as public or protected, otherwise the ASPX page (subclass) cannot be accessedprotected voidPage_Load (Objectsender, EventArgs e) {if(!Page.IsPostBack) { This. Text ="aaaaaaaaaaaaa"; This. Textbox1.databind ();//or this. DataBind (); }}

Three. <%=%>

This format is often used.

<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 here, or a run-time error will occur {return'bbbb' ;}

Four. <%$%>

Primarily used to reference external resources

Special Note: 1. Only server controls are bound

2. Bind only to a property on a server control.

The following is an example of obtaining a Web. config resource:
<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 as expected.
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:p ubs%></asp:textbox>

The same error occurs when running:
"Parser Error message: Do not allow text expressions like" <% $ConnectionStrings:p ubs%> "to be used instead ...

The final emphasis:

<%#%><%#%> is only for data binding of server controls and therefore cannot be mixed with <%=%><%=%> and <%%><%%>

Transferred from: http://www.bdqn.cn/news/201303/8412.shtml

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.