How to include javascript file only once in a WebControl's Render() method?

Posted on 2009-04-27
Last Modified: 2013-11-08
I have a custom WebControl. The webcontrol will include a javascript file.
Problem is when I place the webcontrol multiple times on a page, it includes the javascript twice.

Is there a way to avoid this like a ViewState but one that saves for page state so reincludes on postbacks?

NOTE: The method ClientScript.RegisterStartupScript works, but it places it down the bottom of the page and it needs to be on the top.
Also ClientScript.RegisterClientScriptInclude and ClientScript.RegisterScriptBlock does not seem do anything.
public class AdvancedTextBox : TextBox


        protected override void Render(HtmlTextWriter writer)



            if (true)// HOW TO MAKE THIS ONLY INCLUDE ONCE?




            StringBuilder j = new StringBuilder();

            j.AppendLine("jQuery('textarea#" + this.ClientID + "').autoResize({");

            j.AppendLine("  onResize:function(){  jQuery(this).css({opacity:0.8});  }");

            j.AppendLine("  ,animateCallback:function(){  jQuery(this).css({opacity:1});  }");

            j.AppendLine("  ,animateDuration:300");

            j.AppendLine("  ,extraSpace:40");


            JavaBuilders.JavaString js = new JavaBuilders.JavaString(j.ToString());

            writer.Write(js.ToString(true, true));



Open in new window

Question by:dazkraz
    1 Comment
    LVL 3

    Accepted Solution

    You can check if the Script block is registered or not to the Page ,if the script block is not registered ,then ONLY add it.

    Here testString is the Javascript module made from StringBuilder
    If Not ClientScript.IsClientScriptBlockRegistered("testScript") Then
                ClientScript.RegisterClientScriptBlock(GetType(String), "testScript", testScript.ToString)
            End If

    Open in new window


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    vSphere client error 503 5 56
    network + 7 59
    Parameter for stored procedure 6 40
    VBSCRIPT in Page Load C# ASP.NET 6 41
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now