0

I have written the script dynamically using string builder as follows

public static void ShowMessage1(ENUM_MessageType pMessageType, string pMessage, Button c)
    {
StringBuilder strScript = new StringBuilder();
        strScript.Append("<script type=\"text/javascript\" src=\"").Append("/Scripts/jquery-1.4.1.js").Append("\"></script>");
        strScript.Append("<script type=\"text/javascript\" src=\"").Append("/Scripts/jquery.msgBox.js").Append("\"></script>");
        strScript.Append("<link rel=\"stylesheet\" type=\"text/css\" href=\"").Append("/Styles/msgBoxLight.css").Append("\" />");
        strScript.Append("<script type=\"text/javascript\">");
        strScript.Append("(function example()");
        strScript.Append("{");
        strScript.Append("$.msgBox({");
        strScript.Append("title:'" + lMessageType + "'");
        strScript.Append(",");
        strScript.Append("content:'" + pMessage + "'");
        strScript.Append(",");
        strScript.Append("type:'" + lOptionType + "'");
        strScript.Append(",");
        strScript.Append("buttons: [{ value: 'Yes' }, { value: 'No'}],");
        strScript.Append("success: function (result) {");
        strScript.Append("if(result == 'Yes'){");
        strScript.Append("javascript:_doPostBack('" + c.ClientID + "','');");
        strScript.Append("}");
        strScript.Append("}");
        strScript.Append("});");
        strScript.Append("})();");
        strScript.Append("</script>");
        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            page.ClientScript.RegisterClientScriptBlock(typeof(enumClass), "info", strScript.ToString());
        }
    }

I am getting the exception as ReferenceError: _doPostBack is not defined can some one help me

4

3 回答 3

1

Its should javascript currently you have

strScript.Append("avascript:_doPostBack('" + c.ClientID + "','');");

It should be:

strScript.Append("javascript:__doPostBack('" + c.ClientID + "','');");

Missing j in front. Also make sure that its __ not a single underscore.

于 2013-08-23T18:51:52.073 回答
0

看起来您的__doPostBack()通话缺少下划线。

另外,看看success你渲染的 JS 中的:

(function example() {
    $.msgBox({
        title : 'INFORMATION',
        content : 'I am from client side',
        type : 'confirm',
        buttons : [{
                value : 'Yes'
            }, {
                value : 'No'
            }
        ],
        success : function (result) {
            if (result == 'Yes') {
                javascript : __doPostBack('Button1', ''); // <--- this line
            }
        }
    });
})();

如果您只是想在那里调用回发,请摆脱javascript :它,使其如下所示:

strScript.Append("__doPostBack('" + c.ClientID + "','');");

另外,根据这个 SO question的答案,确保页面上呈现了一个 ASP.NET WebControl。__doPostBack()呈现 WebControl 时会自动包含在页面中。因此,如果您在页面上没有一个,则该__doPostBack()方法可能会丢失。

于 2013-08-23T18:57:15.863 回答
0

如果页面上没有任何 asp.net 服务器端回发控件,则会在客户端上抛出“_doPostBack not defined”错误。为避免上述错误,您可以尝试将以下代码行添加到页面加载事件中:

protected override void OnPreLoad(EventArgs e)
    {
        this.Page.ClientScript.GetPostBackEventReference(this, string.Empty);

        base.OnPreLoad(e);
    }

GetPostBackEventReference 返回一个字符串,该字符串可用于客户端事件以导致回发到服务器

另一种方法是添加隐藏的 asp:Button,它将注册与 GetPostBackEventReference 方法相同的脚本。

于 2013-08-24T09:44:26.863 回答