Javascript with in stringbuilder

2019-08-28 18:43发布

问题:

This code I converted as per needed

http://jquerymsgbox.ibrahimkalyoncu.com/

Hi all I have written the following code to display an alert

public static string ShowAlertMessage(string pHeader, string pError, Page page)
{
    //StringBuilder sb = new StringBuilder();
    StringBuilder strScript = new StringBuilder();

    strScript.Append("<script type='text/javascript' src='Scripts/jquery-1.4.1.js'>");
    strScript.Append("</script>");
    strScript.Append("<script type='text/javascript' src='Scripts/jquery.msgBox.js'>");
    strScript.Append("</script>");
    strScript.Append("<link href='Styles/msgBoxLight.css' rel='stylesheet' type='text/css'>");
    strScript.Append("</link>");
    strScript.Append("<script type='text/javascript'>");
    strScript.Append("function example()");
    strScript.Append("{");
    strScript.Append("$msgBox({");
    strScript.Append("title:" + pHeader + "");
    strScript.Append(",");
    strScript.AppendLine("content:" + pError + "");
    strScript.Append("});");
    strScript.Append("}");
    strScript.Append("</script>");
    page.ClientScript.RegisterStartupScript(page.GetType(), "Script", strScript.ToString(), true);
    return strScript.ToString();
}

But this is not giving me the expected output can some one help me where I went wrong

This is the generated string builder

<script type='text/javascript' src='Scripts/jquery-1.4.1.js'></script>
<script type='text/javascript' src='Scripts/jquery.msgBox.js'></script>
<link href='Styles/msgBoxLight.css' rel='stylesheet' type='text/css'></link>
<script type='text/javascript'>
function example()
{
   $msgBox({title:Testing Code,
   content:Hello
 });
}
</script>

回答1:

Got the answer with this

public static string ShowAlertMessage(string pHeader, string pError)
{
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:'" + pHeader + "'");
strScript.Append(",");
strScript.Append("content:'" + pError + "'");
strScript.Append("});");
strScript.Append("})();");
strScript.Append("</script>");
return strScript.ToString();
}