Introduction to ASP. NET inline expressions in the. NET Framework

Source: Internet
Author: User

This article describes the following ASP. NET inline expressions:

  • <%... %>
  • <% =... %>
  • <% @... %>
  • <% #... %>
  • <% $... %>
  • <% --... %>
  • <% @... %>
<%... %> Embedded code block

Embedded code blocks are used to retain backward compatibility using traditional ASP. The code in the block can execute programming statements and call functions in the current page class during page rendering.

The following example shows an ASP. NET page with the Microsoft Visual basic.net code displayed in the result of a loop in the sample embedded code block:

<%@ Page Language="VB" %>

An embedded code block is always mixed with HTML sources because it is difficult to read and maintain these developers.

For more information about embedded code blocks in ASP. NET web pages, visit the following Microsoft Developer Network (msdn) Website:

Http://msdn.microsoft.com/en-us/library/ms178135.aspxreturn to the top <% =... %> display expression

The <% =... %> display expression is include only response. Write (...) Statement embedded code block is equivalent. This is the easiest way to display information (such as a single string, an int variable, or constant.

For example, the following code example shows the current time:

<%@ Page Language="VB" %>

Remember that the server control attributes cannot be used in the display expression. This is because the. NET Framework directly compiles rather than displaying the property value of the content. This value is used as the value in the entire expression.

For details about how to display information in ASP. NET, visit the following msdn Website:

Http://msdn.microsoft.com/en-us/library/6dwsdcf5.aspxback to the top <% @... %> command expression

Command expressions are syntaxes used to process ASP. NET web forms (. aspx) pages and user controls (. ascx) File Settings when specifying pages and user control compilers.

The ASP. NET page framework supports the following commands:

@ Page Defines page-specific attributes used by ASP. NET page analyzer and compiler. It can be included only in the. aspx file.
* You can use the command name only on the ASP. NET web forms page.
@ Control Defines control-specific attributes used by ASP. NET page analyzer and compiler. It can only be contained in the. ascx file (user control.
* The command name, which can only be in the user control file.
@ Import Explicitly import a namespace to a page or user control.
@ Implementation Declares a page or user control to implement the specified. NET Framework interface.
@ Registration Associate the alias with the namespace and class name. This allows user controls and custom server controls to be included in the requested page or user control during rendering.
@ Assembly Link the assembly to the current page during compilation. It makes all the classes and interfaces of the Assembly available on the page.
@ Master Identifies the ASP. NET master page.
@ Webhandler Identifies an ASP. NET ihttphandler page.
@ Previouspagetype Provides a method to obtain the strong type of the previous page, such as throughPreviouspageAttribute access.
@ Mastertype Assign a class nameMasterASP. NET page of the property. Provides a method to create a strong reference for ASP. NET master pages.
@ Outputcache Control the page of the output cache policy or user control in declarative mode.
@ Reference Link to the current page or user control in a declarative manner.

For more information about instruction syntax, visit the following msdn Website:

Http://msdn.microsoft.com/en-us/library/xz702w3e.aspxback to the top <% #... %> data binding expression

On the page, callDatabindMethod, the data binding expression creates the binding between the server control attributes and the data source.

The following example shows how to bind the string of the function of the Data Binding expression to the tagTextAttribute:

<%@ Page Language="VB" %><script runat="server">    Protected Function SayHello() As String        Return "Hello World"    End Function    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)        lblHello.DataBind()    End Sub</script>

For more information about the data binding expression syntax, visit the following msdn Website:

Http://msdn.microsoft.com/en-us/library/bda9bbfx.aspxback to the top <% $... %> Expression Generator

To set the value of the control property based on the application configuration or the information contained in the resource file, use the expression generator. The basic syntax of expression generator is as follows:

<% $ Expression Prefix: expression %> Value

The dollar sign ($) indicates that the expression below ASP. NET is an expression generator. Expression prefix definition as shown inAppsettings,Connectionstrings, OrResourcesThe type of the expression. In addition, you can create and define your own expression generator. The expression value (:) following the colon is the value of some attributes that ASP. NET will actually use.

The following shows how to use the expression generatorAppsettingsThe node obtains the website copyright in the web. config file and sets the copyright information to text.TextAttribute Value.

In the Web. config fileAppsettingsNode:

<appSettings>    <add key="copyright" value="(c) Copyright 2009 WebSiteName.com"/></appSettings>

Expression generator, in the ASP. NET web forms page:

<div id="footer">    <asp:Literal ID="Literal1" runat="server" Text="<%$ AppSettings: copyright %>"></asp:Literal></div>

For more information about ASP. NET expressions, visit the following msdn Website:

Http://msdn.microsoft.com/en-us/library/d5bd1tad.aspxback to the top <-%-... -- %> server annotation Block

The server comment block allows developers to embed code comments (except in the <SCRIPT> code block) to HTML sources on ASP. NET web forms pages anywhere ). The server does not process any content between the start and end tags of the comments block on the server, or the content is displayed on the generated page.

The following code example demonstrates how to use the server-side comment block on the ASP. NET page:

<%@ Page Language="VB" %><script runat="server">    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)        Dim strName As String        strName = Session("userName")        lblUserName.Text = strName    End Sub</script>

For more information about server annotations, visit the following msdn Website:

Http://msdn.microsoft.com/en-us/library/4acf8afk.aspx
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.