服务器端调用未显示 Fancybox

Fancybox is not showing from Server side call

我必须从服务器端显示 fancybox。我已经编写了一个 javascript 方法并在 ScriptManager.RegisterStartUpScript 中调用了该方法 asp.net button click

下面是我的代码

 protected void btnSaveAndAddConcern_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "fancybox", "callFancyBox();", true);
    }

这是我的 javascript 函数

 function callFancyBox() {
            $("#fancybox").attr("href", "/Agent/AddCampConcernPopup.aspx").fancybox({
                  'width': 550,
                  'height': 200,
                  'type': 'iframe',
                  'title': ''
              }).trigger("click");
          }

函数已调用,但 fancybox 未打开。这个 javascript 函数在我从客户端调用它时工作正常。但是当我从 fancybox 后面的代码中调用它时,它永远不会显示。我检查控制台中的错误,从服务器端调用后显示此错误

"Uncaught TypeError: Cannot read property 'hide' of undefined"

我有 jquery 1.10.2 和 fancybox 版本 1.3.4

同样,当我从客户端调用时,fancybox 工作正常,但问题是服务器端调用。

使用 ScriptManager.RegisterClientScriptBlock 而不是 ScriptManager.RegisterStartupScript

试试这个

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "fancybox", "callFancyBox();", true);

如果加上

就更好了
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ajax", "callFancyBox()", true);

代替

ScriptManager.RegisterStartupScript(this, this.GetType(), "fancybox", "callFancyBox();", true);

同时添加 class 而不是 ID,这样您就可以将一个函数传递给多个函数,如下所示

 $(document).ready(function () { //this line you have to add for initiate script so it will solve your undefined error
            $('.fancybox').fancybox({
                type: 'iframe'                   
            });
 }