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.