【问题标题】:Open Popup with dynamicly generated Html from CodeBehind使用 Code Behind 动态生成的 Html 打开 Popup
【发布时间】:2013-01-11 18:59:36
【问题描述】:

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

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

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

在代码隐藏中我有这个:

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

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

【问题讨论】:

    标签: c# javascript asp.net popup code-behind


    【解决方案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"));
    

    查看详情:Register Client script in ASP.NET

    【讨论】:

    • 但我想尽快打开弹出窗口,因为我有那个 html。
    • @TorbenL。 - 没有得到你..你想在页面加载时打开它??不仅仅是在页面加载函数中编写代码
    • 没有。用户单击一个按钮,完成了一大堆东西,从而产生了一个 html 字符串。一旦我有了这个字符串,我就想打开弹出窗口。我不想等待用户的任何动作。而且我也没有任何链接。只是html。
    • @TorbenL。 - 如果我让你正确,你想在按钮点击时打开弹出窗口,而不是你只需要在文件后面的代码中的按钮点击事件中写下上面的代码......
    • @Pranay_Rana 但它没有打开。代码只是运行,没有任何反应。
    猜你喜欢
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多