Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all I have created a javascript which I want to use in my entire application, so I have created a class which returns string as follows
XML
public static string ShowAlert(string pHeader, string pMessage)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<script language='JavaScript' type='text/javascript'>");
        sb.Append("example1('" + pHeader + "','" + pMessage + "')");
        sb.Append("</Script");
        return sb.ToString();
    }


This is my code JFunction.JS

PHP
function example1(title, content) {
    $.msgBox({
        title: title,
        content: content
    });
}

Now on button click I am just calling as follows
Page.ClientScript.RegisterStartupScript(this.GetType(), "",ShowAlert(pHeader,pMessage) , true);


This example I need to convert in a dynamical way so that it should be called on any page with out writing the function

http://jquerymsgbox.ibrahimkalyoncu.com/[^]
Posted
Updated 1-Jun-13 9:50am
v2

You should create a js file, and then put an include to that file in your master page. Creating javascript in your code behind is always a hack. If you need your 'ShowAlert' method to be accessible from every page, then create your own base class for pages, and put it in there. When I used to do ASP.NET ( it sucks, I do MVC now ), the first thing I'd do is create a custom base page class that returned a strongly typed master page and had other helper methods I wanted access to across my web project.
 
Share this answer
 
Hello,

Try writing ShowAlert() method without script tag as below :

C#
public static string ShowAlert(string pHeader, string pMessage)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("example1('" + pHeader + "','" + pMessage + "');");
        return sb.ToString();
    }
 
Share this answer
 
v2
I got it

C#
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();
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900