ASP.NET Expressions

According to Microsoft, ASP.NET expressions are a declarative way set control properties based on information that is evaluated at run time. For example, you can use expressions to set a property to values that are based on connection strings, application settings, and other values contained within an application's configuration and resource files. Expressions are evaluated at run time when the declarative elements of the page are parsed, and the value represented by the expression is substituted for the expression syntax.
Following are the advantages of ASP.NET expressions

  • You can reduce the code in your application by referencing dynamic information declaratively. This avoids having to write code to set property values at run time.
  • You can reuse the same settings in multiple controls. 
  • You can store sensitive data such as connection string credentials (or other application data) in the Web.config file and then encrypt the data.


Basic syntax for the ASP.NET expressions is
<%$ expressionPrefix: expressionValue %>
Here,  the expression prefix is the type of expression, such as AppSettings, ConnectionStrings, or Resources.


// ConnectionStings
<asp:SqlDataSource id="SqlDataSource1" runat="server">
SelectCommand="SELECT * FROM [Students]"
ConnectionString="<%$ ConnectionStrings:myDbConnectionString1 %>">

// AppSettings
<asp:literal runat="server" text="<%$ AppSettings: version %>"%></asp:literal%>

// Resource Files
<asp:label id="label1" runat="server" text="<%$ Resources: Messages, ThankYouLabel %>"%></asp:label%>
Here, in the App_GlobalResources you must have resource files as Messages.resx,, etc. and in the Message.resx file you must have data with the lable "ThankYouLabel".

Resource files are better to store information specific to a language.

Happy Coding!😊

Reference: Microsoft


    Blogger Comment
    Facebook Comment


Post a Comment