Oct
24
2006
Appsettings expressions
Posted by admin under
ASP.NET 2.0
This is a simple tip, but still might be of value. More of a "good to know you can do it" rather than rocket science.
As you might know, if you have worked with sqldatasources and connectionstrings, you can specify which connectionstring (stored in web.config) to use by using the syntax (in your ASPX file):
<asp:SqlDataSource ID="sql1" runat="server"
SelectCommand="SELECT whetever from whatever"
ConnectionString="<%$ ConnectionStrings:YourConn %>">
</asp:SqlDataSource>
and web.config looking like this:
<configuration>
<connectionStrings>
<add name="YourConn"
connectionString="Server=(local);Database=whatever;uid=sa;pwd=pekka;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
The syntax is section:key and the cool thing about it is that it works for other settings as well. AppSettings for example. I am now developing a Javascript debugging server control to use for my projects - and found that this technique alloes me to turn debugging on and off just by changing web.config - excactly what I needed:
In all my pages I use the following control:
<jslog:JSLog id="idLogSpecial"
runat="server" DebugMode="True"></jslog:JSLog>
And the control will then render the necessery javascripts to setup the log console.
However using a syntax like:
<jslog:JSLog id="idLogSpecial"
runat="server" DebugMode="<% $AppSettings:JsDebugMode %>">
</jslog:JSLog>
allowed me to turn js debugging on and off for all my pages just by changing the web.config appsetting:
<appSettings>
<add key="JsDebugMode" value="false"/>
</appSettings>