为什么 Javascript 代码不能在用户控件中运行?

发布于 2024-08-05 02:19:46 字数 229 浏览 0 评论 0原文

我正在开发一个包含单个页面(home.aspx)的项目。

Home.aspx 包含一个 ScriptManager 和一个 UpdatePanel — 一个带有一些加载相关用户控件的 LinkBut​​ton 的 div。

其中一些用户控件包含 JavaScript。我的问题是 javascript 没有按时运行(在 LinkBut​​ton 单击或 UC 加载后)。

我该如何解决这个问题?

I'm working on a project that contains a single page (home.aspx).

Home.aspx contains a ScriptManager and an UpdatePanel — a div with some LinkButtons that load related UserControls.

Some of those user controls contains javascript. My problem is that the javascript doesn't run on time (after LinkButton click or UC loads).

How do I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青衫负雪 2024-08-12 02:19:46

确保您的脚本已使用 ScriptManager< /a>.

Make sure your script is registered with the ScriptManager.

谎言 2024-08-12 02:19:46

如果用户控件内嵌了JS,则不会运行;通过设置元素的innerHTML 添加的JavaScript(这就是UpdatePanel 所做的)不会运行。

正如 Rick Schott 提到的,如果用户控件使用 ScriptManager 注册其脚本 - 通过在其 ScriptManagerProxy 中包含 asp:ScriptReference 或添加它使用 ScriptManager.Register* 变体之一从其代码隐藏中运行 - 它将运行。

If the user controls have JS inline, it will not run; JavaScript added by setting the innerHTML of an element (which is what UpdatePanel does) does not run.

As Rick Schott mentions, if the user controls register their script with the ScriptManager - either by including asp:ScriptReferences in their ScriptManagerProxy or by adding it from their codebehind using one of the ScriptManager.Register* variants - it will run.

慢慢从新开始 2024-08-12 02:19:46

感谢您的回答,但我已经检查过这个解决方案。
用户控件没有脚本管理器,脚本管理器放在Home.aspx中。
我同时使用 Page.RegisterClientScriptBlock , Page.RegisterStartupScript ,...
但这些都不能正常工作。 UC 中的 Java 脚本代码位于 UC 顶部的 Script 标签中,如下所示:

                   function pageLoad() {
   $addHandler($get("<%=Button2.ClientID%>"), 'click', cp);

}
function cp() {

  /* Do shomthing ... */

}

//*################ JAVASCRIPT 代码结束

在上面的 JS 代码中,Button2 是一个放置在 UC 中的 asp Button。
我今天发现了一个新东西:如果在上面的脚本标签中添加 defer='defer' ,它就会像魅力一样运行!
但这个解决方案是针对 IE(ONLY) 的,所以这不是最好的解决方案。
我还能做什么...?

Thanks For your Answers , But I had examined This solutions .
User Controls Haven't Script Manager , Script Manager Placed in Home.aspx .
I use both Page.RegisterClientScriptBlock , Page.RegisterStartupScript , ...
But These don't work properly . Java script Codes in UC are in Script tag in top of UC Like Below :

                   function pageLoad() {
   $addHandler($get("<%=Button2.ClientID%>"), 'click', cp);

}
function cp() {

  /* Do shomthing ... */

}

//*################ END OF JAVASCRIPT CODES

In above JS Code Button2 is a asp Button placed in UC .
I Find A New Thing today : If add defer='defer' in above Script tag it runs like a charm!
But this solution is for IE(ONLY) , So this isn't the best solution .
What Can i do more ...?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文