用户控制 javascript

User control javascript(用户控制 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)