问题
How to use javascript in .ascx pages
回答1:
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).
回答2:
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.
来源:https://stackoverflow.com/questions/3785816/how-to-use-javascript-in-ascx-pages