Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've done this task before within repeaters and it has worked. However I can't get the below to work for me in a normal webforms page. The images appear as broken links and breakpoints I put in the codebehind are not triggered.

(in the aspx file)

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# GetImageDirectory()%>btnRunReport.png'  />

(codebehind)

public string GetImageDirectory()
{
    return "~/App_Variants/LBSX/images/";
}

This is the second method I've tried, in the other one I tried passing the imagename through as a string, and it would return the entire link that way. Still no luck!

Any thoughts?

Thanks!

[EDIT] Thanks for the help everyone. In the end after the handy hints I found a recursive snippet which did the trick as follows:

private void UpdateImages(Control Parent)
{
    foreach (Control c in Parent.Controls)
    {
        ImageButton i = c as ImageButton;
        if (i != null)
        {
            i.ImageUrl = "~/App_Variants/LBSX/images/" + i.ImageUrl;
        }
        if (c.HasControls())
        {
            UpdateImages(c);
        }
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    UpdateImages(Page);
    ...

Hope it helps someone else.

Cheers

share|improve this question
<%# - call on data-bind event. <%= - call in render (normal usage). you're looking for <%= – RPM1984 Jul 19 '10 at 5:54
Yes, sorry I also have tried the <%= but still no breakpoint triggered. What else could I be doing wrong? – Glinkot Jul 19 '10 at 6:23
Also, when I view source on the page, the control shows the src as: src="&lt;%=GetImageDirectory()%20%>" rather than trying to pass whatever it calculates. Maybe there's a clue there – Glinkot Jul 19 '10 at 6:42

2 Answers

up vote 4 down vote accepted

First, just like Zachary mentioned, you're using the code block for data binding.

Second, as you've already tried, using an inline expression (<%= %>) won't work either in your case, since you can't use an inline expression for any property of a server-tag.

What you could do instead is defining an image button using HTML syntax, omitting the runat="server" tag, and use the inline expression to get your image's URL:

<input type="image" src="<%= GetImageDirectory() %>btnRunReport.png" name="image" />

What an inline expression does is, it calls Response.Write() with the value between <%= %> as the parameter, e.g. <%= this.MyVar %> is Response.Write(this.MyVar).

share|improve this answer
Ok, thanks for that. What I'm trying to do is put a custom directory (depending on certain conditions) in front of the image name. So in one case it may be images3/picture.jpg and in others it would be images4/picture.jpg. I'm not using themes for various reasons. Is there any way in the codebehind I could achieve the same by doing something like: First, set imageURL at designtime to 'picture.jpg' without path Then for each Imagebutton in (whatever the page object is) ImageUrl = "mydirectory" + existing imageurl Would that be workable do you think? Thanks for your time! – Glinkot Jul 19 '10 at 7:14
Thanks to your guidance I've now managed to solve this. Ended up iterating through the controls as mentioned and putting the path at the front. Works a charm! I will add code to the question for others reference. – Glinkot Jul 19 '10 at 7:32
1  
I'm glad you could solve this. I've managed to write the code for iterating through the controls; if somebody's interested, you can find the mentioned code here: hpaste.org/fastcgi/hpaste.fcgi/view?id=27788#a27788 – Giu Jul 19 '10 at 7:47
Thanks for that, much appreciated mate. Note that the one I put above is recursive whereas yours seems to be nested to two levels. Not sure if it ever goes below two levels, mind you... :) – Glinkot Jul 19 '10 at 23:18

Your syntax is for data binding, <%# %>. If you are just trying to do inline c#, you should use <%= %>.

share|improve this answer
Actually I should have mentioned, I've tried that also and no luck still! Good point though, I did find various people saying that when I was searching forums etc. – Glinkot Jul 19 '10 at 6:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.