Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use javascript in .ascx pages

How to use javascript in .ascx pages

like image 665
Asad Avatar asked Sep 24 '10 09:09

Asad


2 Answers

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).

like image 102
Oded Avatar answered Nov 17 '22 02:11

Oded


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.

like image 8
Emrah Avatar answered Nov 17 '22 03:11

Emrah