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%>"></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 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