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

Developer
  • 8,390
  • 41
  • 129
  • 238

3 Answers3

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.

user2711965
  • 1,795
  • 2
  • 14
  • 34
0

Looks like you're missing an underscore on your __doPostBack() call.

Also, take a look at the success in your rendered 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
            }
        }
    });
})();

If you are just trying to call a postback there, get rid of the javascript : so that it reads like this:

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

Also, according to the answer on this SO question, make sure there is an ASP.NET WebControl rendered to the page. __doPostBack() is automatically included on the page when a WebControl is rendered. So, if you on't have one on the page, there's a chance that the __doPostBack() method could be missing.

Community
  • 1
  • 1
valverij
  • 4,871
  • 1
  • 22
  • 35
0

If you don’t have any asp.net server side postback controls on the page the “_doPostBack not defined” error will be thrown on the client. To avoid the described error you can try adding following lines of code into the page load event:

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

        base.OnPreLoad(e);
    }

The GetPostBackEventReference returns a string that can be used in a client event to cause postback to the server

The other approach is to add hidden asp:Button which will register the same scripts as GetPostBackEventReference method.