About how to customize expression builders in ASP. NET 2.0

Source: Internet
Author: User

Expressions is a new feature of Asp.net 2.0. It allows you to easily use custom attributes on the Asp.net page.
On the ASPX page, you only need to use the $ symbol to access the custom attributes.
For example, let's look at an example:
The ASPX page is as follows:

<Asp: sqldatasource id = "sqldatasource1" runat = "server" connectionstring = "<% $ connectionstrings: pubs %>" selectcommand = "select * From catalog"> </ASP: sqldatasource>

The Web. config file is as follows: <configuration>
<Appsettings/>
<Connectionstrings>
<Add name = "pubs" connectionstring = "Server = localhost; database = getwant; trusted_connection = yes"/>
</Connectionstrings>
</Configuration>

The connectionstrings node is available in Web. config by default, So we conveniently Add a pubs attribute to add with ADD.
How can we customize the nodes we use? For example, <% $ version: majorminor %> can be used to display the major and minor version numbers of Asp.net in the current environment?
If we enter the above expression directly on the page, the compiler will tell you that version is not defined. Please customize it in the expressionbuilders node. In fact, the expressionbuilder class will be used at this time.

System. Web. Compilation. expressionbuilderIs the base class of expression builders.
Let's take a look at the settings in Web. config: <compilation DEBUG = "true">
<Expressionbuilders>
<Add expressionprefix = "version" type = "versionexpressionbuilder"/>
</Expressionbuilders>
</Compilation>

How is it easy? Define an expressionprefix to version.
But what do people mean after that type? Is there a versionexpressionbuilder class?
In fact, we inherited the expressionbuilder class. Public class versionexpressionbuilder: expressionbuilder.
{
Public override codeexpression getcodeexpression (boundpropertyentry entry, object parseddata, expressionbuildercontext context)
{
String Param = entry. expression;
If (string. Compare (Param, "all", true) = 0)
{
Return new codeprimitiveexpression (string. Format ("{0}. {1}, {2}. {3}", environment. version. Major, environment. version. Minor, +
Environment. version. Build, environment. version. Revision ));
}
Else if (string. Compare (Param, "majorminor", true) = 0)
{
Return new codeprimitiveexpression (string. Format ("{0}. {1}", environment. version. Major, environment. version. Minor ));
}
Else
Throw new invalidoperationexception ("User $ version: All or $ version: majorminor ");
}
}

At this time, we can compile the following settings on the ASPX page: Asp. net <asp: literal id = "literal1" runat = "server" text = "<% $ version: majorminor %>"> </ASP: literal>

Displayed as "ASP. NET 2.0"
Change the representation to <% $ version: All %> and the format is "ASP. NET 2.0, 50727.42"

Is it very easy...

This article references the custom expression builders chapter in eLearning.

Time is very short. My family urged me to eat, so I will not explain much. If you do not understand, leave a message for me.

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.