用户控制的JavaScript [英] User control javascript

查看:116
本文介绍了用户控制的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2602398/javascript-function-definition-in-asp-user-control\">JavaScript在ASP用户控制功能定义

我将在一个页面中使用两次普通用户控件。有一个与它asscoiated一个JavaScript还需要添加。我这怎么能添加到页面,因为的JavaScript将被添加多次。

I have a generic user control which will be used twice in a page. There is a javascript asscoiated with it which also needs to be added. How can I add this to a page since the javascript will be added multiple times.

推荐答案

该ClientScriptManager是你正在寻找它有注册弦一组RegisterClientScriptxxx方法/ include文件/资源​​等作为客户端脚本块。每一种方法需要一个键和任选的类型的参数,与每个键/类型脚本仅包含一次

The ClientScriptManager is what you are looking for it has a set of RegisterClientScriptxxx methods for registering strings/include file/resources etc. as client script blocks. Each of these methods takes arguments of a key and optionally a type, the script with each key/type are only included once.

在用户控件的onload或OnInit中你要像一个调用以下

In the OnLoad or OnInit of your user control you want a call like the following

Page.ClientScriptManager.RegisterClientScriptInclude(typeof(MyUserControl), "myscript", @"/path/to/my/script.js");

无论用户控件的许多实例是如何在页面脚本只会被包含一次。

No matter how many instances of the user control are in the page the script will only get included once.

BTW,Page.RegisterClientScriptxxx方法现在pcated德$ P $,ClientScriptManager是preferred。

BTW, Page.RegisterClientScriptxxx methods are now deprecated, ClientScriptManager is preferred.

这篇关于用户控制的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆