开发者

Updating an Image in Master Page

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

<asp:Image ID="imgLogo" ImageUrl="~/Images/logo.jpg" runat开发者_StackOverflow社区="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";


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


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;
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜