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

  • Home
  • SEARCH
  • 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

I have created a simple Asp.Net custom control which automatically combines all the correct
I've created a regular ASP.NET user control, including the ascx file. For example: MyUserControl.ascx
I need to implement the classic Factory Method pattern in ASP.NET to create server
I'm trying to run a freshly created ASP.NET Website using C#, however when I
I just created a ASP.NET MVC project in VS2008. When I press F5 to
I'm pretty sure stackoverflow.com is created with ASP.NET, but no matter where I click
I'm interested in seeing what custom extensions other developers have created for the ASP.NET
In ASP.NET 3.5 (with IIS6), are AppDomains are created for every request? I know
I created a GridView in an ASP.NET application and used the Auto Format tool
I created a protected folder on my ASP.NET site with the ASP.NET Configuration tool.

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.