How can a control inject a style (or javascript reference) in the page HEADer?

  • Thread starter ~~~ .NET Ed ~~~
  • Start date
N

~~~ .NET Ed ~~~

That is the question indeed!. It is not the first time I make some sort of
web control that depends on some
CSS styles. Usually one would go around using the CssStyle property of the
HTML markup to do some
inline styles. But my situation is that these styles I need:

a) are a little too big to do inline
b) the same CSS style is used in multiple tags, thus doing it inline
clutters the output
c) By having it global (in the HEAD part) it is made available for *all* the
instances of the
control on the same page.

So, how do I go about having my custom control "inject" either a <style>
section, or a <link rel...>
reference to a stylesheet in the HEAD section? This would come pretty handy
to me but so far
I have not seen this.

Along the same lines, the same issue arises when a control uses some "heavy"
JavaScript that is better
"concealed" (the less clutter the better) in a reference to a javascript
stored in a separate file by
adding the reference to the .js in the HEAD section. This issue has come to
me in the past as well.

Any input is greatly appreciated.

Thx,
E.
 
J

John Saunders

~~~ .NET Ed ~~~ said:
That is the question indeed!. It is not the first time I make some sort of
web control that depends on some
CSS styles. Usually one would go around using the CssStyle property of the
HTML markup to do some
inline styles. But my situation is that these styles I need:

a) are a little too big to do inline
b) the same CSS style is used in multiple tags, thus doing it inline
clutters the output
c) By having it global (in the HEAD part) it is made available for *all*
the
instances of the
control on the same page.

So, how do I go about having my custom control "inject" either a <style>
section, or a <link rel...>
reference to a stylesheet in the HEAD section? This would come pretty
handy
to me but so far
I have not seen this.

<HEAD runat="server" id="myHead">

works in ASP.NET 2.0, but I'm not sure about 1.0 or 1.1. If it works (and
you might have to declare

protected HtmlGenericControl myHead;

) then you should be able to do:

myHead.Controls.Add(new LiteralControl( said:
Along the same lines, the same issue arises when a control uses some
"heavy"
JavaScript that is better
"concealed" (the less clutter the better) in a reference to a javascript
stored in a separate file by
adding the reference to the .js in the HEAD section. This issue has come
to
me in the past as well.

Take a look at Page.ReqisterStartupScript and
Page.RegisterClientScriptBlock.

John Saunders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top