The difference between <% #> and <% => shows and binds data.

Source: Internet
Author: User

I haven't come to the csdn blog for a long time. TodayCommunityI can see a question about "<% # %>" in Asp.net, so I still wantArticlePost it to your blog. Although it is not a very profound problem, you should pay more attention to it ......

AspxPage,<% = %>And<% # %>Difference

Most of ASP. NET Some of them are mentioned in the textbooks. <% = %> And <% # %> The difference is that the binding time is different, <% # %> Is called in the control Databind Function. For <% = %> I think you should be familiar ASP Times, it is equivalent Response. Write . In ASP. NET The same is true of the times. For the moment, ignore what is mentioned in the textbook and put the "binding time" aside. <% = %> And <% # %> The difference is: the former is the output, and the latter is the value assignment! That is:

<% = "A" %>Equivalent:Response. Write ("");

<% # "A" %>Equivalent to: Variable= "";

Let's look at an example:

<Html>

<Body>

<ASP: button Text=<%# "Hello, the Internet! " %> Runat= "Server"ID= "Testbutton"/>

</Body>

</Html>

This sentence is equivalent to a value assignment. Translate the above blacklisted sentence into the background.CodeThat is:Testbutton. Text = "hello, the Internet! ";

Example 2:

<Html>

<Body>

<%= "Hello, the Internet! "%>

<%# "Hello, the Internet! "%>

</Body>

</Html>

<% = "Hello, the Internet! "%>It is equivalent:Response. Write ("hello, the Internet! ");

Then the second<% # %>How can this problem be explained? If, according to my "assignment", who does it assign the value?

In fact, the translated background code is as follows:(New system. Web. UI. literalcontrol (). Text = "hello, the Internet! ";

A piece of text. Although it is not included between any start and end labels[Font = 'courier] ASP. NET [/font]It is also considered as a control. One[Font = 'courier] literalcontrol [/font]Control. So,[Font = 'courier] <% # %> [/font]Is to assign the value[Font = 'courier] literalcontrol [/font]ControlTextAttribute.

I used a class to generate static HTML code. The htmltextwriter class is encapsulated. And I did tracking debugging. I was surprised by the tracking results. ASP. NET regards all the tags that are not marked with the "runat = server" attribute as a literalcontrol control. For example, in the preceding HTML code, for example, in the HTML code at the beginning of the article, ASP. NET considers that there are three server controls: literalcontrol, button, and literalctontrol. The text value of the first literalcontrol is " hello, theinternet !", The text value of the last literalcontrol is " ". That is to say, <% = %> should be translated before <% # %> ......

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.