开发者

Taking action for all controls with an 'ImageUrl' property

开发者 https://www.devze.com 2023-01-08 15:50 出处:网络
I\'ve got the below piece of code, which iterates through the controls on a webpage and where it is an imagebutton, it modifies the imageUrl property.What I\'d like it to do, is go through each contro

I've got the below piece of code, which iterates through the controls on a webpage and where it is an imagebutton, it modifies the imageUrl property. What I'd like it to do, is go through each control and, if that control happens to have an imageUrl property, then do the change. Eg normal images etc would be included in the process. I get the feeling that generics or something might be the key here, but I am not versed in that area to say the least. Any thoughts? Thanks!

        public static void AddThemePathToImages(Control Parent)
    {

        foreach (Control c in Parent.Controls)
        {
            ImageButton i = c as ImageButton;
            if (i != null)
            {
                // See if theme specific version of this file exists.  If not, point it to normal images dir.
                if (File.Exists(System.W开发者_运维问答eb.HttpContext.Current.Server.MapPath("~/App_Variants/" + GetUserTheme().ToString() + "/images/" + i.ImageUrl)))
                {
                    i.ImageUrl = "~/App_Variants/" + GetUserTheme().ToString() + "/images/" + i.ImageUrl;
                }
                else
                {
                    i.ImageUrl = "~/images/" + i.ImageUrl;
                }   
            }
            if (c.HasControls())
            {
                AddThemePathToImages(c);
            }
        }
    }


You don't need generics, just regular inheritance will work fine:

Since ImageButton just inherits ImageUrl from its parent class of Image, you could just change your first bit from:

ImageButton i = c as ImageButton;

to:

Image i = c as Image;

Then it'll work fine for both Image and ImageButton

Now, if you wanted it to work for any controls that have an ImageUrl property (even some custom control that doesn't derive from Image) then you'll need to use reflection to see if the object has a property of ImageUrl and if so, use it.


I would consider creating a sub classed custom control "ThemedImageButton" that can encapsulate the code for setting the correct ImageUrl on each image.

You can add an override OnPreRender to check for the existence of the image and update the property.

You can then do a simple find & replace through your project to replace <asp:ImageButton /> with <xxx:ThemedImageButon />.

public class ThemedImageButton : ImageButton
{
    protected override OnPreRender(EventArgs e)
    {
        // See if theme specific version of this file exists.  If not, point it to          normal images dir.
            if (File.Exists(System.Web.HttpContext.Current.Server.MapPath("~/App_Variants/" + GetUserTheme().ToString() + "/images/" + i.ImageUrl)))
            {
                i.ImageUrl = "~/App_Variants/" + GetUserTheme().ToString() + "/images/" + i.ImageUrl;
            }
            else
            {
                i.ImageUrl = "~/images/" + i.ImageUrl;
            }   
    }
}

You can also provide the same solution for <asp:Image /> controls as well.


If you do not have the property set, then I think imageUrl will null or empty string. Try to put this in a 'if' statement. I think that will solve your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号