更新母版页中的图像

发布于 2024-08-20 12:45:30 字数 424 浏览 3 评论 0原文

我在主页上有一个图像控件(用于徽标),如下所示:

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

我允许我的用户上传他们自己的徽标。因此,在他们上传徽标的页面上,我想用新上传的徽标替换现有徽标。我尝试了以下方法,但没有一个起作用:

1)做了 Response.Redirect 返回同一页面,但不起作用。

2)图像上传完成后尝试以下代码,但仍然不起作用:

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

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";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撧情箌佬 2024-08-27 12:45:30

尝试结合使用这两种方法:执行 Response.Redirect 并在页面加载时替换 ImageUrl。

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

南汐寒笙箫 2024-08-27 12:45:30

在我的机器上工作正常 - 确定文件最终位于您认为的位置吗?

或者,操作完成后您正在做什么?您是否在 OnLoad 或类似中设置 ImageUrl ?您在执行此操作时是否检查是否处于回发状态?

代码:

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文