Adding scripts to a user control

S

Scott M.

You can include script tags in the HEAD as well as the BODY of a web page
and since your user control will wind up being placed inside another's
document's BODY tags, you're all set to just write them.

The only reason (and it is a good one) that script tags are usually placed
in the head section is so that they can be loaded before everything else.
As long as the scripts you'll be referencing don't get caught up in that,
you can just place them in your div tag.

-Scott
 
K

K Viltersten

When i design a web user control, i usually
put everything in a div, like this:

<@ control ...>
<div> ... </div>

Now, i need to do some tricks with jQuery,
which requires me to reference some JS-files
but i'm not sure where to do so.

Usually, i'd put the references in <head>
but how do i do that when there's no such
tag (as in the case with user control)?

I tried to simply putting the <head> tag in
"by brute force" but regrettably, i didn't
get the behavior i expected.
 
S

Sreenivas

Put the script tag in Head section of the Page or MasterPage(if you
use) ;in which you place the web user control.
Yes, scott's answer would suffice :)
 
S

Scott M.

The OP's script needs to be in the user control, not a specific page.

-Scott


Put the script tag in Head section of the Page or MasterPage(if you
use) ;in which you place the web user control.
Yes, scott's answer would suffice :)
 
G

Gregory A. Beamer

The OP's script needs to be in the user control, not a specific page.

The advice is still correct (oe one "coirrect" way), although not
complete. There are two ways to accomplish what Sreenivas has stated:

1. Use one of the register statements (RegisterStartupScript,
RegisterClientSideScript)

2. Programatically add to the head of the page

In addition, if the OP knows which pages need jQuery, he can add the
directive there, even if the control does not emit. This is not very
useful for a commercial control (one he is selling), but works for his
own site (yes, it is sloppier than registering the script). If jQuery is
used throughout the site, setting it up in the master page is also fine.

Peace and Grace,
 
G

Gregory A. Beamer

When i design a web user control, i usually
put everything in a div, like this:

<@ control ...>
<div> ... </div>

Now, i need to do some tricks with jQuery,
which requires me to reference some JS-files
but i'm not sure where to do so.

Usually, i'd put the references in <head>
but how do i do that when there's no such
tag (as in the case with user control)?

I tried to simply putting the <head> tag in
"by brute force" but regrettably, i didn't
get the behavior i expected.

There are two basic options here:

1. Register client side scripts (or startup scripts)
2. include in head of page or master page

The option you choose depends on a few factors. If this is a commercial
control, you are probably better to register the scripts, but leave an
option to turn this off, as a user may already be using jQuery for
something else (as Microsoft is heavily pushing it).

If this is not commercial, you have choices. If jQuery is used
throughout the site, put it in the master page. If only on a few pages,
you can put it in the head section of the page. If the page inherits
from a master page, you can still register in the head if you have a
head content region (the default since 2.0).

Peace and Grace,
 

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,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top