ClientScript.RegisterClientScriptBlock

I want to add javascript in header on page load. The javascript has to be defined in separate class.
How do I make following to work:


public class JavascriptCSS
{
    public static HtmlGenericControl GoogleAPI()
    {
        HtmlGenericControl javascript = new HtmlGenericControl("script");

        javascript.Attributes.Add("type", "text/javascript");
        javascript.Attributes.Add("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js");

        return javascript;
    }
}


Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "GoogleAPI", JavascriptCSS.GoogleAPI(), true);

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "GoogleAPI", JavascriptCSS.GoogleAPI(), true);  

Change true to false. Your HtmlGenericControl is already generating the script tags, however, I doubt that will work because you are returning a control and trying to present it as a string. You might be able to override the ToString() implementation, but I’d argue that isn’t worth it.

Instead consider just return the HTML

public class JavascriptCSS 
{ 
    public static HtmlGenericControl GoogleAPI() 
    { 
        return "<script type=\\"text/javascript\\" src=\\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js\\"></script>"; 
    } 
}  

And

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "GoogleAPI", JavascriptCSS.GoogleAPI(), false);