Detailed usage examples in the form of <%> In Asp.net

Source: Internet
Author: User

The code separation mode in Asp.net is certainly not unfamiliar. The code written in C # (or other languages) is generally not mixed with the Design Language HTML, but sometimes it cannot be avoided.
<%> Will be used to bind the display and variable data on the UI page. Today I saw an article dedicated to this analysis, and I made some corrections:
1. <%>
This format is actually the same as ASP, except that VBSCRIPT or JavaScript code is used in ASP, and the language supported by. NET is used in Asp.net.
Note: The Server Control cannot contain the <%> syntax. Otherwise, an error occurs.
The following code is displayed on the. ASPX page:
<%
Int A = 2;
Int B = 3;
Int c = A + B;
Response. Write (C );
%>
Ii. <% # %>
This format is exclusive to Asp.net. It is the control data binding syntax, and must call the databind () method of the control to execute (or the entire page. databind () is to call the databind () method for all the obtained controls)
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 string text; // note that this field must be declared as public or protected. Otherwise, the ASPX page (subclass) cannot be accessed.
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
This. Text = "aaaaaaaaaaa ";
This. textbox1.databind (); // or this. databind ();
}
}
Effect of Running

Iii. <% = %>
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 a return value must be returned. Otherwise, a runtime error may occur.
{
Return "BBBB ";
}
Running effect:

Iv. <% $ %>
Used to reference external resources
Note: 1. Only server controls can be bound.
2. It can only be bound to a property of the server control.
To obtain the Web. config resource for an instance, follow these steps:
<Asp: textbox runat = "server" id = "cc" text = "<% $ connectionstrings: pubs %>"> </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>
In this way, it can run normally.
If you modify files in aspx: Use the client control

<Input type = "text" value = "<% $ connectionstrings: pubs %>"/>
Or: do not bind to a property of the server control.

<Asp: textbox runat = "server" id = "cc"> <% $ connectionstrings: pubs %> </ASP: textbox>

The same error will occur during running:
"Analyzer error message: a text expression similar to" <% $ connectionstrings: pubs %> "cannot be used. use another one ......

 
Finally, we stressed:
<% # %> Is only applicable to Data Binding of server controls. Therefore, it cannot be used together with <% =%> and <%>.

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.