In Asp.net, what is the meaning and difference between, and?

Source: Internet
Author: User
In Asp.net, HTML code containing the form of <%> is often displayed, which generally contains the following formats: 1. <%> This format is actually the same as ASP, but it is only VBSCRIPT or JavaScript code in ASP, and in Asp.net.. NET platform. Note: The Server Control cannot contain the <%> syntax (C # code is used here) <% int A = 2; int B = 3; int c = A + B; response. write (c); %> 2. <% # %> in this format, it is unique in Asp.net. It is the control data binding syntax and must call databind () of the control () method (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 the server control can use the <% # %> syntax (inaccurate) when the entire page calls this. when the databinder () method is used, both the Server Control and the client control are bound to <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> Aspx. CS code: protected string text; // note that the value must be public or protected. Otherwise, the ASPX page (subclass) cannot access protected void page_load (Object sender, eventargs E) {If (! Page. ispostback) {This. TEXT = "aaaaaaaaaaaaa"; this. textbox1.databind (); // or this. databind () ;}} when this. textbox1.databind () run the displayed result (you cannot insert an image and test it yourself) This. databind () Running the displayed effect (you cannot insert an image and test it yourself) 3. <% = %> 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 stri Ng displaystr () // note that a return value must be included here; otherwise, a runtime error will occur {return "BBBB";} 4. <% $ %> This form is mainly used for the web. key-value pairs in the config file are bound: 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 a 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> run will see the same error. Last Note: <% # %> only for 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: Publ IC string displaystr () // note that a return value must be included here; otherwise, a runtime error will occur {return "BBBB";} if so, A running 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.

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.