User control javascript(用户控制 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用户控制 javascript
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
