Format usage in Asp.net (original)

Source: Internet
Author: User
Today, I opened my blog on the first day, and I am very happy because I can study technology with my friends in the garden. I hope to share the accumulated and learned knowledge in projects with my friends in the garden. Make some effort for the growth of our garden. This is my second topic. I hope to help those who are not familiar with the <%> syntax and give your valuable comments to those who are familiar with it.
In Asp.net, HTML containing the form <%> is often displayed. Code I have collected the data here. In general, it includes 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.
(Here I use C # code)

<%
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 =   " Aaaaaaaaaaaaa " ;
This . Textbox1.databind (); // Or this. databind ();
}
}

Effect of Running

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 there must be a return value here; otherwise, a runtime error will occur.
{
Return "BBBB";
}

Running effect:

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 >

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 errors will occur during running.

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

From: http://www.cnblogs.com/Charles2008/archive/2008/01/24/1051853.html

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.