In asp.net, & lt ;%#%& gt;, & lt ;%=%& gt; and & lt ;%%& gt; what are their meanings and differences,

Source: Internet
Author: User

In asp.net, what are the meanings and differences between <% # %>, <% = %>, and <%>,

In asp.net, html code containing the form <%> is often displayed, which generally contains the following formats:
1. <%>
This format is actually the same as asp, but it only contains vbscript or javascript code in asp, and is supported by the. net platform in asp.net.
Note: The Server Control cannot have the <%> syntax.
(C # code is used here)
<%
Int a = 2;
Int B = 3;
Int c = a + B;
Response. Write (c );
%> 2. <% # %>
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 () means that the DataBind () method is called for all the obtained controls. In this case, both the Server Control and the client control are bound)
Note: Only server controls can use the <% # %> syntax (inaccurate)
When the this. DataBinder () method is called on the entire page, both the Server Control and 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 the Code 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 ();
}
}
When this. TextBox1.DataBind () runs the displayed effect (you cannot insert an image and test it yourself)
This. DataBind () run the displayed results (you cannot insert an image and test it yourself)
Iii. <% = %>
This form is actually extended by <%>. it is equivalent to Response. write (<%>) (not standard, but can understand), it can also 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 a return value must be returned. Otherwise, a runtime error will occur.
{
Return "bbbb ";
}
Iv. <% $ %>
This form is mainly used to bind the key-value pairs of the web. config file: strings used to connect to the database
Note: 1. Only server controls can be bound.
2. It can only be bound to a property of the server control.
<Asp: TextBox runat = "server" ID = "cc" Text = "<% $ ConnectionStrings: pubs %>"> </asp: TextBox> web. the config file is as follows: <connectionStrings>
<Add name = "pubs" connectionString = "Server =.; database = pubs; uid = sa; pwd =" providerName = "System. Data. SqlClient"/>
</ConnectionStrings>.
If you modify files in aspx: Use the client Control <input type = "text" value = "<% $ ConnectionStrings: pubs %>"/> or: not bound to a server control attribute <asp: TextBox runat = "server" ID = "cc"> <% $ ConnectionStrings: pubs %> </asp: TextBox>
The same errors will occur during running.
Last note:
<% # %> Is only applicable to Data Binding of server controls. Therefore, it cannot be used together with <% =%> and <%>.
Supplement:
For: <% = %> Format:
Another situation:
Aspx code: <input type = "text" id = "a" value = <% = DisplayStr () %>/>
<Input id = "Text1" type = "text" runat = "server" value = <% = DisplayStr () %>/> aspx. cs code:
Public string DisplayStr () // note that a return value must be returned. Otherwise, a runtime error may occur.
{
Return "bbbb ";
} In this case, a runtime error occurs. Finally, I would like to emphasize the usage of this <% = %>: Try to use this form for client controls. After all, it is a common syntax for asp and will not cause problems.
However, server controls of asp.net may not be fully suitable.

Related Article

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.