用户控制 javascript [英] User control javascript

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

问题描述

可能重复:
ASP用户控件中的JavaScript函数定义

我有一个通用的用户控件,它将在一个页面中使用两次.有一个与之相关的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 方法,用于将字符串/包含文件/资源​​等注册为客户端脚本块.这些方法中的每一个都接受一个键的参数和一个可选的类型,每个键/类型的脚本只包含一次.

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.

顺便说一句,Page.RegisterClientScriptxxx 方法现已弃用,首选 ClientScriptManager.

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

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

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