js在updatePanel中的奇怪问题

strange issue with js in updatePanel

本文关键字:问题 updatePanel js      更新时间:2023-09-26

我有一个链接在一个更新面板调用一个js方法做当前窗口的打印:

<a href="javascript:print()" id="cpMain_popupCouponDescriptionControl_hrefPrint">Print Coupon</a>

在Page_Load事件中调用js方法:

private void loadJs()
{
    String flashMap = "script";
    if (!Page.ClientScript.IsClientScriptBlockRegistered(this.Page.GetType(), flashMap))
    {
        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), flashMap, "function print(){ alert('test'); window.print(); return false; }", true);
    }
}

当按下链接时,警告窗口多次显示(在我点击"确定"之后,这似乎很奇怪),但从未调用window.print()(未打开用于打印的新选项卡)。

如果我直接从href调用javascript:window.link,它工作,但因为链接包含在更新面板中,它不再工作第二次(这就是为什么我试图注册脚本)。

有人能看出这里的问题吗?

我相信window.print();实际上是递归地调用您的print()函数。