1

I found that even I use ResolveUrl to indicates the path, it still does not work unless I add javascript in the page that user control is embeded in.

<script src="<%= ResolveUrl("~/JS/jquery.js") %>" type="text/javascript"></script>
<script type="text/javascript">
 $(document).ready(function(){
       alert("aaaaa");
});
</script>

Is there a way to add JS into user control directly?

4
  • Just to clarify, are we talking about an ASP.NET user control? Commented Aug 17, 2011 at 22:17
  • JavaScript is always available (except when it is disabled). You are loading a JavaScript file, the jQuery library to be more specific. Of course it is not doing anything on its own, you have to tell it what to do. But it is there. Commented Aug 17, 2011 at 22:17
  • jquery doesn't do anything until you call it. Just linking to it merely loads the library so you can use it. Commented Aug 17, 2011 at 22:18
  • 1
    What is emitted in your HTML when you view source? Commented Aug 17, 2011 at 22:18

2 Answers 2

1

If you're talking about creating UserControl-specific javascript, I know exactly what you're talking about. There's no real easy way to do it (aside from just adding a script tag with your script and/or src), so here's what I do:

  1. Create your user control
  2. Create your js specific to that control in a separate .js file
  3. At the top of your user control, put a big flowerbox type comment that lists all the file dependencies for that control. This includes CSS (maybe you have your css separated out too) and any JS files.
  4. When you drop in a control, get in the habit of opening it up first and taking a look at your dependencies. This will ensure that you drop in the correct files too.

Also, naming your separate JS file the same thing as your User Control helps.

Sign up to request clarification or add additional context in comments.

Comments

0
<input type="checkbox" onclick="alert('you clicked me!') />

is one tiny sample. Beyond that, we'd need details of what you mean by "control".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.