How to read web.config APP key settings in HTML markup - asp.net

I have an ASP.NET site which uses a 3rd party activeX control. I have to pass a few parameters to the OBJECT tag in the HTML page. If i hardcode these parameters into the HTML everything works.
I would like to place the parameters in my web.config with app settings "key/value" pairs.
My problem is i cannot read the app key setting in the HTML markup to succesfully pass them in as parameters. I can read them fine from server side code behind.
What's the correct way to read these settings in the client side HTML markup ?
Thanks

In addition to using <%=ConfigurationManager.AppSettings["MyAttribute"]%>, as others have noted, you can also use expression builders. The syntax is a little different. Instead of <%=...%> you use <%$ AppSettings: MyAttribute %>, like so:
<object id="myObjectID attr="<%$ AppSettings: MyAttribute %>" ...>
If you are just dumping an appSettings value directly into static HTML (as I presume you are in this example), these two approaches are identical for all practical purposes.
What is nice about expression builders, though, is that you can use them to declaratively assign appSettings values to Web control properties, something you cannot do with the <%=...%> syntax. That is, with expression builders you can do something like:
<asp:Label runat="server" ... Text="<%$ AppSettings: MyAttribute %>" />
Whereas you could not do:
<asp:Label runat="server" ... Text="<%=ConfigurationManager.AppSettings["MyAttribute"]%>" />

The following code:
<%$ AppSettings: MyAttribute %>
is not compatible with general HTML markup and JavaScript function! It's good for asp tag.
Whereas
<%=ConfigurationManager.AppSettings("MyAttribute")%>
really work in general HTML markup.
so
<%=ConfigurationManager.AppSettings("MyAttribute")%>
is my recommendation!

You can use the ConfigurationManager in you ASPX page. Then you can add in your OBJECT tag parameters:
Web.Config
</configuration>
<appSettings>
<add key="Setting" value="Value"/>
<appSettings>
</configuration>
ASPX
<object>
<param name="Setting" value="<%= System.Configuration.ConfigurationManager.AppSettings["Setting"] %>" />
</object>

I suggest you generate your OBJECT tag dynamically at run-time from the server. This way you can inject whatever parameters you read from the web.config file.

You have a few options. If you add the runat="server" attribute to your object tag, you can access it from your codebehind using its ID, and add attributes that way:
myObjectID.Attributes.Add("attrName", "value")
If you don't want to do that, you could use inline literals:
<object id="myObjectID attr="<%= ConfigurationManager.AppSettings("MyAttribute") %>" ...>
Either way should get the job done.

Related

ASP.net HTMLDecode not working for Resource entries

I have a ASP.net MVC project, which utilizes resource entries (.resx) through out the project.
Few the resources fed, have HTML in it
example: Hello <b>World!</b>
With paragraphs href and more. As the resources are stored in an XML, the entries are HTMLEncoded
i.e the above example looks like this
eg: Hello <b>World!</b>
Due to this, wherever the resources are displayed, the HTML formatting does not render, and instead the HTML is displayed as visible text.
I tried to use HttpUtility.HTMLDecode and Server.HTMLDecode, but both wont work.
What is wrong? Any other work around resources?
Both of the following work fine for me:
<%= Resource.MyResource %><br />
<asp:Label runat="server" Text="<%$ Resources:Resource, MyResource %>" /><br />
A Resource entry such as <b>Text</b> is displayed in bold by the browser.
Some Controls do automatic HTML encoding of their inputs. Could that be what's happening for you?

Why does databound property of ASP.NET usercontrol work only without quotes?

I have a custom ASP.NET user control implemented fully in code-behind. The control has one string property and its declarative markup looks like this:
<uc:MyControl ImageUrl="/Content/images/" runat="server" />
Most likely the actual declaration will use data binding syntax like this:
<uc:MyControl ImageUrl="<%# PageInfo.ImageUrl %>" runat="server" />
Now here's the odd part pertaining to my question. If I use the above syntax, the data binding does not work and the value of ImageUrl at run-time is the string literal of whatever is between quotes. However, if I remove the double quotes, it works as expected:
<uc:MyControl ImageUrl=<%# PageInfo.ImageUrl %> runat="server" />
The same behavior occurs with both double and single quotes. I am puzzled by this and although the code is working it's really not optimal as putting values in quotes is the norm and the approach to make the data binding work is decidedly unorthodox.
Anyone have any ideas on why this only works without quotes?
I found the problem...it was something to do with the file (probably encoding, but not sure). On a whim, I copied the code to a new file and deleted the original. Now the data binding works with the quotes as it should.

ASP.NET ExpressionBuilder syntax - output AppSetting within img tag

I would like to use ASP.NET's ExpressionBuilder syntax to dynamically retrieve domain of static content from an AppSetting.
I am using the following syntax, which does not work:
<img src="<%$Appsettings:STATIC_CONTENT_DOMAIN %>/img/logo.jpg" alt="logo" width="176" height="159" />
FYI, the desired HTML output is:
<img src="http://static.myserver.com/img/logo.jpg" alt="logo" width="176" height="159" />
Please note, I cannot use <%= %> syntax because my ASPX page needs to be CompilationMode="never". (The reason I am using ExpressionBuilder syntax is that it works in no-compile pages)
Any ideas on how I can do this?
This approach worked for me (not very readable :)...
<img src="<asp:Literal runat='server' Text='<%$Appsettings:STATIC_CONTENT_DOMAIN%>'/>/img/logo.jpg" />
You might want to consider writing a custom expression builder - they're not too difficult to write. Here are some tutorials:
Express Yourself With Custom Expression Builders
Expression Builders in ASP.NET 2.0
You could have your own expression syntax such as:
<%$ MyCdnUrl: Static, '/img/logo.jpg' %>
Then you'd parse out everything after the ":" and build up the URL that you need.
I think that expression builders must be used as "property values" so you can't use them completely on their own. You'll still have to use something like <img runat="server"> or an <asp:Image> control or an <img> with the <asp:Literal> inside it.
I believe you need to use a server-side asp.net control, such as:
<asp:Image ID="MyImage" runat="server" ImageUrl="<%$Appsettings:STATIC_CONTENT_DOMAIN %>" />
I don't know if you can combine the statement with static info like you have, such as:
<asp:Image ID="MyImage" runat="server" ImageUrl="<%$Appsettings:STATIC_CONTENT_DOMAIN %>/img/logo.jpg" />
My guess would be it isn't possible, but I guess it's worth a shot. Try it out and see...

referencing appSettings from usercontrols

In my .ascx usercontrol i'm trying to dynamically generate links using a value i've stored in web.config.
link
and when i try running, i get a parser error
Literal expressions like '<%$appSettings.MYPATH %>' are not allowed. Use <asp:Literal runat="server" Text="<%$appSettings.MYPATH%>" /> instead.
I know i'm probably missing something relatively minor.
<%= ConfigurationManager.AppSettings["myKey"] %>
EDIT:Don't forget the =
link
should work (it at least does on the IIS server I use). (Unfortunately it's more verbose)
Try this instead
.ascx
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
in code behind
Literal1.Text = "link"
More accurate answer will be next:
Link
Use a colon instead of a dot and add runat="server":
link
The documentation isn't very clear on this point, but ASP.Net Expressions are for use within server tags. Thus, if you want to use one in a plain html tag, you must add runat="server" so that the tag is processed on the server where the expression will be evaluated.

Dynamically set the DefaultValue of a ParameterBinding in a DataFormWebPart

In my custom aspx page in WSS I am using a DataFormWebPart with an XSL file to render some data. In order to pass values to the XSL I use parameter bindings. Specifically, I need to pass in the server host URL like this:
<ParameterBinding
Name="HttpHost"
Location="CAMLVariable"
DefaultValue="http://hardcoded.com" />
This works fine, but the next thing I want to do is to get the host name dynamically. So figuring out how to get that from SharePoint I added the following binding:
<ParameterBinding
Name="HttpHost"
Location="CAMLVariable"
DefaultValue='<%# SPContext.Current.Site.Url.Replace
(SPContext.Current.Site.ServerRelativeUrl, "") %>' />
Now to the problem. The code works as expected if used some other place in the page, but with the above code SharePoint reports:
Web Part Error: The 'ParameterBindings' property of 'WebPartPages:DataFormWebPart'
does not allow child objects.
Anyone have a take on this?
I have enabled server side code according to SharePoint 2007: using ASP.NET server side code in your pages
After trying various methods of manipulating the ParameterBindings property without success I thought of how I could get the dynamic value in there using the Location attribute.
The ParameterBinding Location attribute refers to where to fetch the value from. Articles like this hints of the "Control()" option. So changing the parameter binding to:
<ParameterBinding
Name="HttpHost"
Location="Control(MyHttpHost, Text)"
DefaultValue="" />
and adding the following code to my page:
<asp:TextBox ID="MyHttpHost" runat="server" Visible="false" />
<script runat="server">
protected void Page_Load()
{
MyHttpHost.Text =
SPContext.Current.Site.Url.Replace(SPContext.Current.Site.ServerRelativeUrl, "");
}
</script>
...actually did the trick!
To get to the parameter values from within the accompanying XSL file I put param elements in the root element. The param name attribute must match that of the ParameterBinding:
<xsl:stylesheet ...>
...
<xsl:param name="HttpHost"/>
The parameter can then be referenced as any other XSL variable.

Resources