ASP.net 2.0 Web Forms syntax Guidance

Source: Internet
Author: User
Tags bind execution expression functions include insert new features variables
asp.net|web| syntax

The ASP.net Web Forms page is a declarative text file with an. aspx extension. In addition to static content, you can also use eight different syntax tag elements. This section reviews these syntactic elements and provides examples of how to use them.

Syntax for rendering code:%%> and <%=%>

The code rendering block is represented by the <% ...%> element, which allows you to control what is rendered, executed during the display phase of the Web Forms page execution. The following examples show how to use them to loop through the contents of HTML.

<%@ Page language= "VB"%>
<body>
<% Dim I as Integer
For I = 0 to 7%>
<font size= "<%=I%>" > Hello world! </font> <br>
<% Next%>
</body>
<%.%> contains code that is executed only, and an expression that contains an equal sign (<%= ...%>) evaluates the result when the content is displayed. Therefore, <%= "Hello World"%> with C # code <% Response.Write ("Hello World"); %> displays the same result.
Note that because languages need to use tags to terminate or detach statements (such as semicolons in C #), it is important to place these tags correctly.

C # code

<% Response.Write ("Hello World"); %> need to terminate the statement with a semicolon.

<%= "Hello World"; %> error: Causes "Response.Write" ("Hello World";); "

<%= "Hello World"%> do not need a semicolon.

Syntax for declaring code: <script runat= "Server" >

The code declaration block defines the member variables and methods that will be compiled into the page class. These blocks can be used to create page and navigation logic. The following example shows how to define the Subtract method in the <script runat= "Server" block and then call it in the page.

<script language= "VB" runat=server>
Function Subtract (Num1 As Integer, Num2 as Integer) As Integer
Return num1-num2
End Function
</script>

<body>
<%
Dim number as Integer = 100
Do While number > 0
Response.Write ("Value:" & Number & "<br>")
Number = Subtract (number, 1)
Loop
%>
</body>
Note: Unlike ASPs, functions must be defined in <%%> blocks in ASP-all functions and global variables must be defined using the <script runat=server> tag. The function declaration in the <%%> block prompts the syntax to compile the error message.

Server Control Syntax

Custom asp.net server controls allow page developers to dynamically generate HTML user interfaces and respond to client requests. They are expressed in a declarative, markup based syntax in a file. These tags are different from some of the other tags, and they contain a "runat=server" attribute. The following example shows how to use the <asp:label runat= server control in a asp.net page. This control corresponds to the label class in the System.Web.UI.WebControls namespace.

By adding a tag with an ID of "message", you can create a label instance at run time:

<asp:label id= "message" font-size=24 runat= "Server"/>
We can use this name to access the control. The following code sets the Text property of the control.

Message.Text = "Welcome to asp.net"
<script language= "VB" runat=server>
Sub Page_Load (Sender as Object, E as EventArgs)
Message.Text = "Welcome to asp.net"
End Sub
</script>
<body>
<asp:label id= "message" font-size=24 runat=server/>
</body>

HTML server Control syntax

HTML server controls allow developers to programmatically manipulate HTML elements in a page. The HTML server control label differs from the client HTML element, and it has a "runat=server" attribute. The following example shows how to use the Html<span runat=server> server control in a asp.net page.

<script language= "VB" runat=server>
Sub Page_Load (Sender as Object, E as EventArgs)
message.innerhtml = "Welcome to asp.net"
End Sub
</script>
<body>
<span id= "message" style= "font-size:24" runat=server/>
</body>
Data binding syntax: <%#%>

Asp. NET built-in ability to support data binding allows page developers to bind control properties to data container values hierarchically. The code in the <%#%> code block executes only when the DataBind method of its own parent control container is invoked. The following example shows how to use data-binding syntax in a <asp:datalist runat=server> control.

In this list of data, each item specifies a template. The content of the item template is specified using a data-binding expression, Container.DataItem points to the data source used by the MyList data list.

<asp:datalist id= "MyList" runat=server>
<ItemTemplate>
Here is a value: <%# Container.DataItem%>
</ItemTemplate>
</asp:datalist>
In this case, the data source for the MyList control is programmed, and then the DataBind () method is invoked.

Calling a control's DataBind method raises a recursive tree (the underlying control from the control to the tree); The DataBinding event for each server control in that hierarchy is thrown, and the data-binding expression in the control evaluates the value accordingly. Therefore, if the page's DataBind method is invoked, then every data-binding expression in the page is invoked.

<script language= "VB" runat=server>
Sub Page_Load (Sender as Object, E as EventArgs)
Dim Items as New ArrayList
Items.Add ("one")
Items.Add ("two")
Items.Add ("Three")
Mylist.datasource = Items
Mylist.databind ()
End Sub

</script>
<body>
<asp:datalist id= "MyList" runat=server>
<ItemTemplate>
Here is a value: <%# Container.DataItem%>
</ItemTemplate>
</asp:datalist>
</body>
ASP.net 2.0 also contains a new, simplified data-binding syntax that allows controls to automatically bind data to data source controls without calling DataBind () in the page code. This syntax is discussed in the "Performing Data Access" section.

Object tag syntax: <object runat= "Server"/>

Object tags allow page developers to declare and create variable instances using declarative, markup-based syntax. The following example shows how to use object tags to create an instance of a ArrayList class.

The object is automatically established at run time and can be accessed through the id "items".

<script language= "VB" runat=server>
Sub Page_Load (ByVal Sender as Object, ByVal E as EventArgs)
Arrayitems.add ("one")
Arrayitems.add ("two")
Arrayitems.add ("Three")
Mylist.datasource = Arrayitems
Mylist.databind ()
End Sub
</script>

<body>
<object id= "Arrayitems" class= "System.Collections.ArrayList" runat=server/>
<asp:datalist id= "MyList" runat=server>
<ItemTemplate>
Here is a value: <%# Container.DataItem%>
</ItemTemplate>
</asp:datalist>
</body>
Server-side annotation syntax: <%--Comment--%>

Server-side annotations Enable page developers to block the execution and rendering of server code, including server controls, and static content. The following example shows how to block the execution of content and send it to clients. Note that all information between <%--and--%> is filtered out and can only be seen in the original server file, even if it contains other asp.net directives.

<body>
The below content has been hidden from browser clients using a server-side comment
(View the. aspx source to what we mean:-)
<%--
<asp:calendar id= "Mycal" runat=server/>
<% for I = 0 to 44%>
Hello World <br>
<% Next%>
--%>
</body>
Server-side file contains syntax: <--#Include file= "Locaton.inc"-->

Server-side files contain (#Include) allow developers to insert the contents of a specific file anywhere on the asp.net page. The following example shows how to insert a custom caption and footnote in a page.

<body>
!--#Include file= "Header.inc"-->
<BR/>
<BR/>
!--#Include file= "Footer.inc"-->
</body>
Expression syntax: New features in the <%$ ...%>2.0

ASP.net 2.0 adds a new declarative expression syntax for value substitution before page parsing. It is useful when we need to replace a server control property value with a connection string value in a Web.config file or an application setting. When localized (locaization), it can also be used to replace values in a resource file.

<asp:sqldatasource id= "SqlDataSource1" connectionstring= ' <%$ connectionstrings:pubs '%> ' server ' runat= = "Sp_getauthors"/>
<asp:label id= "Label1" text= ' <%$ resources:exchrate, Convertlabel%> ' runat= ' server '/>

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.