2

我想动态设置 image.imageurl ......但是当页面不在母版页上时,下面的代码才有效。

这是代码:

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    Byte[] bytes = AsyncFileUpload1.FileBytes;
    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);

    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "img",
        "top.document.getElementById('Image1').src='data:image/jpg;base64," + base64String + "';",
        true);
}
4

2 回答 2

1

在 theMasterPageId派生自ContentPlaceHolder,例如:

ctl00$ContentPlaceHolder1$Image1

但是你为什么不简单地使用Image1.ClientId呢?

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "img",
    "top.document.getElementById('" + Image1.ClientId + "').src='data:image/jpg;base64," + base64String + "';",
    true);
于 2013-02-28T09:33:32.817 回答
0

Tim Schmelter 是正确的,您还可以将控件上的 ClientIDMode 设置为 Static。例如

<asp:Image runat="server" ID="Image1" ClientIDMode="Static" />

MSDN 文档:http: //msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx

于 2013-02-28T12:08:57.027 回答