SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict itHighway's Avatar
    Join Date
    Jan 2008
    Location
    GUJ
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP Code:
    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;
        }

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

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Code:
    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
    Code:
    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
    Code:
    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "GoogleAPI", JavascriptCSS.GoogleAPI(), false);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •