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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:19:15+00:00 2026-05-23T06:19:15+00:00

Consider to develop a web control (ASP.NET). What you would really like to do

  • 0

Consider to develop a web control (ASP.NET). What you would really like to do is styling and developing this control in a very good way, here’s a very good way to do this (this is how I would like to do, further in this question I will explain why I cannot do this).

A programmatic approach

1) I create my control in a separate folder called WebControls and I name it (for example) MyWebControl. I will have these files: MyWebControl.ascx and MyWebControl.ascx.cs.

2) Given that my control is a complex control I associate a style and a dynamic client behavior referencing, in the control html, a css stylesheet called MyWebControl.ascx.css and a javascript file called MyWebControl.ascx.js.

3) In my control I do the following:

<%@ Control Language="C#" 
            AutoEventWireup="true" 
            CodeFile="MyWebControl.ascx.cs" 
            Inherits="MyApp.WebControls.MyWebControl" %>

<link href="MyWebControl.ascx.css" rel="stylesheet" type="text/css" />
<script src="MyWebControl.ascx.js" type="text/javascript"></script>

<div>
...
</div>

This is it!

The problem

Well there is a problem in this thing: when my control is rendered, and in a page there is more than one of this control of mine, I get the links to the css and js file duplicated or even repeated more than once.

How to link an external stylesheet/javascript file in my control without occurring in this bad stuff?

EDIT

OK, after looking a bit, with the help of others here in the community, I could understand that Page.ClientScript is what comes to the rescue.

However, there are a lot of functionality for a script to be registered… can you tell the difference among these?

1) Page.ClientScript.IsClientScriptBlockRegistered

2) Page.ClientScript.IsClientScriptIncludeRegistered

3) Page.ClientScript.IsOnSubmitStatementRegistered

4) Page.ClientScript.IsStartupScriptRegistered

And the corresponding set methods?

1) Page.ClientScript.RegisterClientScriptBlock

2) Page.ClientScript.RegisterClientScriptInclude

3) Page.ClientScript.RegisterOnSubmitStatement

4) Page.ClientScript.RegisterStartupScript

Furthermore: can this be applied to javascript and css too?

Thankyou

  • 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-23T06:19:16+00:00Added an answer on May 23, 2026 at 6:19 am

    One thought: wouldn’t it be possible to use ClientScriptManager.RegisterClientScriptBlock to inject the css-file import?

    Something like(not tested):

    if(! Page.ClientScript.IsClientScriptBlockRegistered("MyWebControl.ascx.css"))
    {
        Page.ClientScript.RegisterClientScriptBlock(this.getType(),"MyWebControl.ascx.css",@"<style type=""text/css"" src=""MyWebControl.ascx.css""></style>");
    }
    

    Edited to change RegisterStartupScript to RegisterClientScriptBlock

    According to your edits:

    • RegisterStartupScript(type, key, script)
    • RegisterClientScriptBlock(type, key, script)

    The difference between these two methods is where each one emits the script block. RegisterClientScriptBlock() emits the script block at the beginning of the Web Form (right after the tag), while RegisterStartupScript() emits the script block at the end of the Web Form (right before the tag).

    To better understand why there are two different methods for emitting client-side script, realize that client-side script can be partitioned into two classes: code that is designed to run immediately when the page is loaded, and code that is designed to run when some client-side event occurs. A common example of code that is designed to run when the page is loaded is client-side code designed to set the focus to a textbox. For example, when you visit Google, a small bit of client-side code is executed when the page is loaded to automatically set the focus to the search textbox.

    http://msdn.microsoft.com/en-us/library/aa478975.aspx#aspnet-injectclientsidesc_topic2

    Edit: from your comments i’m assuming that it unfortuately does not work this way. Have a look at following links:

    • http://forums.asp.net/t/557140.aspx/1?CSS+stylesheet+in+ascx+file/?post=2692177
    • http://nathanaeljones.com/146/referencing-stylesheets-scripts-from-content-pages/

    There might be some working approaches

    As a hint, you could create the HtmlLink programmatically in Page_Init-Handler of your UserControl:

    Dim objLink As New HtmlLink();
    objLink.ID = "MyWebControlascxcss";
    objLink.Attributes("rel") = "stylesheet";
    objLink.Attributes("type") = "text/css";
    objLink.Href ="~/filname.css";
    Page.Header.Controls.Add(objLink);
    

    You should remember to check first with a recursive function if the link was already added to Page.Header.Controls-Collection.

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

Sidebar

Related Questions

After having read Ian Boyd 's constructor series questions ( 1 , 2 ,

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.