How to use javascript in .ascx pages
Asked Answered
C

2

7

How to use javascript in .ascx pages

Carrie answered 24/9, 2010 at 9:36 Comment(1)
by the <script> tag, perchance? is there anything more specific you're wondering?Grussing
W
19

You can add script tags to the markup:

<script type="text/javascript">
   // place your javascript here
</script>


<script type="text/javascript" href="path to js file" />

Or use ScriptManager in the code behind so you don't include the same file/js section multiple times if you use multiple controls in a page.

You can find out more about ScriptManager here (overview, including usage scenarios).

Washrag answered 24/9, 2010 at 9:42 Comment(0)
K
8

I'm assuming that you included the JavaScript code within script tags in the user control page.

I'm also assuming that you are calling a function from one of the controls within the user control

Another assumption I'm making is that your user control is hidden at the loading time.

If the user control is in an UpdatePanel and/or its Visible attribute is set to false by default, you will get "Object expected" error because simply your script is not loaded when the page loads and the function you are calling is non-existent.

Workaround for this is to use style="display:none" instead of Visible="false" for your user control in the main page.

Katzenjammer answered 28/12, 2010 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.