为什么ScriptManager.RegisterClientScriptBlock在内容页面中不起作用?

时间:2013-02-28 09:25:03

标签: asp.net scriptmanager

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

以下是代码:

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

2 个答案:

答案 0 :(得分:1)

MasterPage Id ContentPlaceHolder来自ctl00$ContentPlaceHolder1$Image1 ,例如:

Image1.ClientId

但为什么不简单地使用ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "img", "top.document.getElementById('" + Image1.ClientId + "').src='data:image/jpg;base64," + base64String + "';", true);

{{1}}

答案 1 :(得分:0)

Tim Schmelter是正确的,你也可以将控件上的ClientIDMode设置为Static。 E.g。

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

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

相关问题