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

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

Who is Participating?
DreamsTechConnect With a Mentor Commented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.