Setting imageURL using a function in ASP.NET

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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).

Method 2

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

Method 3

I give you another solution. Use ExpressionBuilder :

  1. Create a class devired from ExpressionBuilder and override function GetCodeExpression
     namespace your.namespace
    {
    public class CustomBuilder : ExpressionBuilder
    {
        public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
        {
            Type type1 = entry.DeclaringType;
            PropertyDescriptor descriptor1 = TypeDescriptor.GetProperties(type1)[entry.PropertyInfo.Name];
            CodeExpression[] expressionArray1 = new CodeExpression[1];
            expressionArray1[0] = new CodePrimitiveExpression(entry.Expression.Trim());
    
            String temp = entry.Expression;
            return new CodeCastExpression(descriptor1.PropertyType, new CodeMethodInvokeExpression(new
           CodeTypeReferenceExpression(base.GetType()), "GenerateLink", expressionArray1));
        }
        public static  String GenerateLink(String link)
        {
            return ConfigurationManager.AppSettings["MediaPath"] + link + "?ver=" + ConfigurationManager.AppSettings["MediaCode"];
        }
    }
    }

expressionArray1 is the input array for GenerateLink function. You can change the size of array according to numbers of input params for your function

2.Register your expression in webconfig

<system.web>
    <compilation debug="true" targetFramework="4.0" >
      <expressionBuilders>

        <add expressionPrefix="GenLink" type="your.namespace.CustomBuilder"/>
      </expressionBuilders>

    </compilation>

3.In view your can use new expression:

<asp:ImageButton ID="ImageButton1" runat="Server" ImageUrl='<%$ GenLink:images/magnifier.jpg %>'/>

4.Enjoy !!!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x