Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 577045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:06:40+00:00 2026-05-13T14:06:40+00:00

I created an asp.net Server control that derives from a LinkButton, and renders a

  • 0

I created an asp.net Server control that derives from a LinkButton, and renders a small javascript function to the page.

I want to use this control many times on the page, but just want the javascript to be rendered once on the page. On the other hand, I rather not manually add it in a js file, because i don’t want to have a chance of forgetting to add the js file in the future.

What will be the best way to go about doing this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:06:41+00:00Added an answer on May 13, 2026 at 2:06 pm

    Preventing duplication in client scripts

    Use a registration method such as Page.ClientScript.RegisterClientScriptBlock(..). There are a couple of overloads but they all work in a similar fashion. It’s smart enough to render the script only once no matter from how many control instances it’s issued.

    Microsoft remarks in the MSDN documentation:

    A client script is uniquely identified
    by its key and its type. Scripts with
    the same key and type are considered
    duplicates. Only one script with a
    given type and key pair can be
    registered with the page. Attempting
    to register a script that is already
    registered does not create a duplicate
    of the script.

    The MSDN docs also contain sample code such as this:

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
    {
      StringBuilder cstext2 = new StringBuilder();
      cstext2.Append("<script type=text/javascript> function DoClick() {");
      cstext2.Append("Form1.Message.value='Text from client script.'} </");
      cstext2.Append("script>");
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
    }
    

    Other similar registration methods

    Different methods can be used to register your client script in different ways – see each of the ClientScriptManager.RegisterClientScript*(..) methods in MSDN for more detail about each. They’re all built to prevent duplication in the rendered page.

    This gives you the freedom to output scripts at the control level without worrying or having to track or count scripts.

    Edit:

    The ClientScriptManager instance is accessed through the Page.ClientScript property.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I've created a ASP.Net page that has a dropdown control with autopostback set
I have a HiddenField control that is created within my ASP.NET server control. I
I have created four ASP.NET Server controls that will inject a lot of JavaScript
I have created a master page in asp.net which dont have any server side
I have web page in ASP.NET. I have created Tree View control with jQuery.
I have the following jQuery that does find the ASP.NET server control (tested by
What is the replacement for a server control in ASP.NET MVC? What I want
I have a SharePoint2010 site that I have created an ASP.NET menu control for.
I've created a custom server control in ASP.NET to render a standard checkbox and
I have created a REST server under ASP.NET and I can't figure out the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.