ASP.Net – How to use expression binding on client side for appSettings, ConnectionStrings, and resource files.

To bind your label control on your aspx page with a value in your web config or resource file, you can use expression binding. The value is retrieved from the named element within the <%$ … %> tags:

Get data from appSettings in Web.config
<asp:Label ID=”lblAppSettings” runat=”server” Text=”<%$ appSettings:LabelText %>”></asp:Label>

Get data from ConnectionStrings in Web.config
<asp:Label ID=”lblConnectionStrings” runat=”server” Text=”<%$ ConnectionStrings:NorthwindConnectionString %>”></asp:Label>

Get data from resource file MyResoruceFile.resx in App_GlobalResources
<asp:Label ID=”lblResourceFile” runat=”server” Text=”<%$ Resources:MyResourceFile, City %>”></asp:Label>

Web.Config

<appSettings>
<add key="LabelText" value="This is the text of Label 1" />
</appSettings>

<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=MY-LAPTOP;Initial Catalog=Northwind;User ID=sa;Password=PASSWORD" providerName="System.Data.SqlClient" />
</connectionStrings>

ResourceFile.resx
String = City Value = Ocala

The results when your aspx page is displayed are as follows:

label 1 = This is the text of Label 1
label 2 = Data Source=MY-LAPTOP;Initial Catalog=Northwind;User ID=sa;Password=PASSWORD” providerName=”System.Data.SqlClient
label 3 = Ocala

Leave a Reply