russds
russds

Reputation: 875

Settings the ImageUrl in an ImageButton on a master page with an AppSettings key

I'm trying to display the ImageUrl of an asp:ImageButton using AppSettings in my master page like this:

 View.Master:
 ....
 <asp:ImageButton ID="aspShowHideButton" ImageUrl='<%# System.Configuration.ConfigurationManager.AppSettings("HomeDirectory").ToString()%>images/arrowUpButton.gif' runat="server" />

Unfortunately when I pull this up in the browser, this is the code that's being rendered:

 <input type="image" name="ctl00$ctl00$aspContentMain$aspShowHideButton" id="aspContentMain_aspShowHideButton" onmouseover="ShowHideButtonMouseOver()" onmouseout="ShowHideButtonMouseOut()" src="../%3C%25#%20System.Configuration.ConfigurationManager.AppSettings(%22HomeDirectory%22).ToString()%25%3Eimages/arrowUpButton.gif" />

So it's taking my ImageUrl literally, but I want it to take the value of the key, which is:

  ...
  <appSettings>
        ....
        <add key="HomeDirectory" value="/" />
        ....

I've tried, removing the "ToString()" function, I've tried "#" and "$" infront of the System.Configuration.... statement. I've also tinkered with trying to get this to work in the Page_Load function, using:

 Protected Sub Page_Load(....)
    If Not IsNothing(Master.FindControl("aspShowHideButton")) Then
       Dim ShowHideButton As ImageButton = Master.FindControl("aspShowHideButton")
       ShowHideButton.ImageUrl = System.Configuration.ConfigurationManager.AppSettings("HomeDirectory") + "images/arrowUpButton.gif"
    End If

But that didn't seem to work either, I'm assuming because it could not find the control I was looking for (ie. aspShowHideButton).

Fundimentally, i want to have a key/value pair in my web.config file that allows me to change where my images are, and I want to be able to use this key/value pair in an ImageButton:ImageUrl on my master page, which seems like it must be a pretty popular thing to do. Any advise, direction, is appreciated!

Thanks!

Upvotes: 0

Views: 7079

Answers (2)

daniloquio
daniloquio

Reputation: 3902

For using appsettings in a server tag, use this syntax:

<%$ AppSettings:HomeDirectory %>

But you won't be able to concatenate the sufix of ImageUrl. Notice that if you want to refer home directory in an asp.net server control, ~ will do the trick.

 <asp:ImageButton ID="aspShowHideButton" ImageUrl="~/images/arrowUpButton.gif" runat="server"/>

The easy solution

Initialize the ImageUrl property on server side code, say for example the Page_Load event. There you will be able to use whatever server code you want.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
       this.aspShowHideButton.ImageUrl = System.Configuration.ConfigurationManager.AppSettings("HomeDirectory").ToString() + "images/arrowUpButton.gif";
    }
}

Now, if you really need to define that sort of concatenation in the ImageButton tag directly, you will need to use the Code Expression Builder. Read about that here.

With Code Expression Builder, you will be able to use this kind of syntax in your ImageButton tag:

ImageUrl="<%$ Code: System.Configuration.ConfigurationManager.AppSettings("HomeDirectory").ToString() + "images/arrowUpButton.gif" %>"

Upvotes: 2

coder
coder

Reputation: 13250

You can try this:

<asp:ImageButton runat="server" ImageUrl="<%$ AppSettings:FullPath %>images/image001.jpg" ></asp:ImageButton>

(Or) there is a small work aroud for this to work:

Web.Config:

<appSettings> 
<add key="testKey" value="images/up.gif" />
</appSettings>  

Add an image button:

<asp:ImageButton ID="ImageButton1" runat="server" />

And from codebehind you can call this way:

 protected void Page_Load(object sender, EventArgs e)
    {
        this.ImageButton1.ImageUrl = ConfigurationManager.AppSettings["testKey"];
    }

Upvotes: 1

Related Questions