1

在某个时刻,我的代码隐藏中有一个动态生成的带有 html 的字符串。
我想打开一个以该 html 为源的弹出窗口。

我尝试了以下方法:
我在我的.aspx网站上创建了这个方法(Javascript):

    function OpenWindowWithHtml(html, title) {
            var myWindow = window.open('', title);
            myWindow.document.write(html);
            myWindow.focus();
     }  

在代码隐藏中我有这个:

    Response.Write("OpenPopupWithHtml(\"" + html + "\", \"" + title + "\");");  

但是当我尝试执行此操作时出现错误。
有没有人看到,我在这里做错了什么?
或者有人知道更好的方法吗?

4

1 回答 1

1

编辑

在按钮上单击它应该是这样的

protected void btnAbct_Click(object sender, EventArgs e) {
   ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "OpenPopupWithHtml('" + html + "', '" + title + "');");
}

执行代码,即。你用javascript写的函数

 ClientScript.RegisterStartupScript(this.GetType(),
   "newWindow", "OpenPopupWithHtml('" + html + "', '" + title + "');");

你可以像这样注册客户端脚本

if (!ClientScript.IsClientScriptBlockRegistered("exampleScript"))
    ClientScript.RegisterStartupScript(this.GetType(), "exampleScript","
<script language = "'javascript'">
alert('you just registered the start up script')
</script>
");

来自 asp.net 文件背后的代码

要打开弹出窗口,只需替换上面代码中的这一行

 ClientScript.RegisterStartupScript(this.GetType(),
   "newWindow", String.Format("<script>window.open('{0}');</script>",
         "mypage.html"));

详细检查:在 ASP.NET 中注册客户端脚本

于 2013-01-11T09:56:57.547 回答