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 211469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:05:53+00:00 2026-05-11T18:05:53+00:00

I have an ASP.NET user control (.ascx file). In this user control I want

  • 0

I have an ASP.NET user control (.ascx file). In this user control I want to use a .js file.

So I include <script src="file.js" type"text/javascript"></script> on the page.

However, sometimes I use this user control in a webpage where this same script has already been loaded.

How can I add a <script /> declaration that will render on the page only if the page doesn’t already contain another <script /> tag for the same script?

  • 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-11T18:05:54+00:00Added an answer on May 11, 2026 at 6:05 pm

    As you are using asp.net, it makes sense to do the check server-side as that will be where you choose to add the reference to the javascript file. From your .ascx file you could register with the following:

    this.Page.ClientScript.RegisterClientScriptInclude("GlobalUnqiueKey", UrlOfJavascriptFile);
    

    … from your page you just call the ClientScript object directly:

    ClientScript.RegisterClientScriptInclude("GlobalUnqiueKey", UrlOfJavascriptFile);
    

    The ‘GlobalUniqueKey’ can be any string (I use the url of the javascript file for this too)

    If you try to register a script with the same key string, it doesn’t do anything. So if you call this in your page, your control or anywhere else, you’ll end up with only one reference in your page. The benefit of this is that you can have multiple instances of a control on a page and even though they all try to register the same script, it is only ever done a maximum of one time. And none of them need to worry about the script being already registered.

    There is a ‘IsClientScriptIncludeRegistered(stringkey)’ method which you can use to see if a script has already been included under that key but it seems pretty redundant to do that check before registering as multiple attempts to register do not throw exceptions or cause any other errors.

    Doing the check client-side means that, assuming the multiple javascript references are cached by the browser (they may not be), you still have multiple tags and the over head of each one causing some javascript to run. If you had 20 instances of your control on a page, you could get serious issues.

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

Sidebar

Related Questions

I have an asp.net user control on which I want to use jquery ajax.
I have an asp.net user control, userControl1.ascx, and another user control, userControl2.ascx. userControl2 is
I have a asp.net usercontrol (.ascx). the user control has a HTML hidden field.
I have an ASP.NET user control that contains a text box control and a
I have a ASP.NET user control which hosts a 'HtmlImage'. The src attribute is
I have a simple asp.net user control: <%@ Control Language=C# AutoEventWireup=true CodeBehind=QueryDefinitionItem.ascx.cs Inherits=xxx.UserControls.QueryDefinitionItem %>
I have a simple ASP.NET Web User Control. It looks like this: <%@ Control
I have an ASP.Net web user control that contains a TextBox and a calendar
We have created a User Control for ASP.Net. At any one time, a user's
(New to ASP.NET here.) I have a user control which should check for a

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.