views:

75

answers:

2

I have an Image control (for logo) on the master page as follows:

<asp:Image ID="imgLogo" ImageUrl="~/Images/logo.jpg" runat="server" />

I am allowing my users to upload their own logos. So on the page where they upload their logo I want to replace the existing logo with the newly uploaded logo. I tried the following but none of them worked:

1) Did a Response.Redirect back to the same page and it didn't work.

2) Tried the below code after the image upload is complete and even this didn't work:

Image imgLogo = (Image)Master.FindControl("imgLogo");
            imgLogo.ImageUrl = "~/Images/newLogo.jpg";
A: 

Try combining both approaches: do a Response.Redirect and replace ImageUrl on page load.

Li0liQ
A: 

Works okay on my machine - sure the file is ending up where you think it is?

Alternatively what are you doing after the action is complete? Are you setting ImageUrl in OnLoad or similar? Are you checking that you're not in a postback when doing so?

Code:

    public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string filename = string.Format("~/Images/{0}.jpg", Guid.NewGuid().ToString());
        FileUpload1.SaveAs(MapPath(filename));

        Image imgLogo = (Image)Master.FindControl("imgLogo");
        imgLogo.ImageUrl = filename;
    }
}
Rob Ashton