views:

804

answers:

4

The Scenario

I have an ASP.NET web project. I want to be able to define all of the links for the site inside my web.config file so that they can be changed easily if needs be. Currently I have an "" section in my web.config file.

The Question

How do I bind this key value pair to an '' tag in my .aspx file?!

The App Settings in My Web.Config File

<appSettings>
    <add key="MyNewLink" value="http://someurl.co.uk/" />
</appSettings>

Help greatly appreciated.

EDIT:

Sorry I should have mentioned that this is for a html link: **<a href></a>**

+2  A: 

Hi,

Isn't this what a .sitemap file is for?

Anyway, as far as I know, you will have to 'bind' this from code behind. Something like:

hlYourLink.NavigateUrl = ConfigurationManager.AppSettings["MyNewLink"];
DavidGouge
so i would have to give the <a href=""></a> an ID attribute called 'h1YourLink'?
Goober
And a runat="server" .. But the above solution Locksfree provided should work.
Moulde
Oh, and you should use the asp:hyperlink control, because the a tag doesn't have a property called navigateurl
Moulde
+3  A: 

In your aspx file it would be:

NavigateUrl='<%$ AppSettings:MyNewLink %>'
Locksfree
This fails......###Error Message:Literal expressions like '<%$ AppSettings:MyNewLink %>' are not allowed. Use <asp:Literal runat="server" Text="<%$ AppSettings:MyNewLink%>" /> instead.
Goober
This definitely works:<a runat="server" href="<%$ AppSettings:MyNewLink %>">Text link</a>
Locksfree
Well you learn something new every day, I didn't know you could do that! Thanks @Locksfree
DavidGouge
A: 

I ended up using this......

.aspx file

<asp:literal id="litgetquote" runat="server"></asp:literal>

.aspx.cs CODE BEHIND

litgetquote.Text = "<A HREF='" + ConfigurationManager.AppSettings["GetQuoteUrl"] + "'>" +
            "get a quote now" + "</A>";
Goober
you should consider using this for readability:litgetquote.Text = string.Format("<A HREF='{0}'>get a quote now</A>", ConfigurationManager.AppSettings["GetQuoteUrl"]);
Pete Amundson
A: 

Thank you for posting this solution !

My boss told me to put all the href addresses in the web config file so it would be easy to change them in the future.

I thought no problem but then nothing I tried worked.

This solution is perfect for what I need.

lady