eliza sahoo

eliza sahoo

  • NA
  • 41
  • 0

Using ASP.NET expression to bind the control properties at run time

Mar 22 2010 10:16 AM

Following are some use cases and examples for this:

a) Common use of this is to specify the connection string property of a control.


<asp:SqlDataSource ID="SqlDataSource1" Runat="server" 
SelectCommand="SELECT * FROM [Employees]"
ConnectionString="<%$ ConnectionStrings:TraineeDatabaseConnectionString%>">
</asp:SqlDataSource>


Inside Web.config:


<!--A common use of expressions is to set the connection string property of a control-->

<connectionStrings>
<add name="TraineeDatabaseConnectionString"
connectionString="uid=trainee;pwd=mindfire;database=Trainee2009;server=LANSERVER;"
providerName="System.Data.SqlClient" />
</connectionStrings>



b) To display static content in our web pages we can add key-value pair in <appSettings> .
For example, Lets set a key value pair inside Web.config for the copyright message for our website.


<!--setting the site's copyright message-->

<appSettings>
<add key="copyright" value="(c)Copyright 2009 MFS"/>
</appSettings>

Now we can set the property like


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


We can use this key "copyright" where ever we want. We can do the same in our masterpage also, but the advantage here is the setting in a centralized place. Web.config files are easy to access and understand. It will be more helpful in case of Nested master page concept.


c) In addition to the above two, you can display values that are stored in resource (.resx or .resource) files in our project. Resource files are used to store information for a specific language or "language and culture" combination. Suppose we want that welcome message in our web page should display according to the language specified at run time, then we can use this.

Steps for using Resource file :
1. Create a App_GlobalResources folder in the root directory.
2. Create a file WelcomeMessage.resx.
3. Now create the Name Value pair as Name: Welcome, Value:Welcome to our website!!!
4. Now set it as property of control as follows:


<asp:Label id="lblResource" runat="server" text="<%$ Resources: My_Resource, Welcome %>" />

 

Hope this will help you.
Searching has no end points. Keep searching.