如何在 .ascx 页面中使用 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3785816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:09:20  来源:igfitidea点击:

How to use javascript in .ascx pages

.netjavascriptasp.netjqueryascx

提问by Asad

How to use javascript in .ascx pages

如何在 .ascx 页面中使用 javascript

回答by Oded

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 ScriptManagerin the code behind so you don't include the same file/js section multiple times if you use multiple controls in a page.

或者在后面的代码中使用ScriptManager,这样如果在页面中使用多个控件,就不会多次包含相同的文件/js 部分。

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

您可以ScriptManager在此处了解更多信息(概述,包括使用场景)。

回答by Emrah

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

我假设您script在用户控制页面的标签中包含了 JavaScript 代码。

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 UpdatePaneland/or its Visibleattribute is set to falseby 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.

如果用户控件在一个UpdatePanel和/或它的Visible属性被false默认设置为,你会得到“ Object expected”错误,因为当页面加载时你的脚本没有加载并且你调用的函数不存在。

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

解决方法是在主页中使用style="display:none"而不是Visible="false"用于您的用户控件。