在 asp.net 中创建具有 javascript 属性和方法的自定义控件
作为创建自定义控件的起点,我想制作一个仅显示数字的控件。如果我们想象 .ascx 文件除了文字控件外不包含任何内容,并且后面的代码将该值设置为 1。
然后我希望能够执行 myControl.increment(); 这将运行一些 javascript 来增加文字控件的值。
我可以将 javascript 注入到页面中,例如伪代码:
page_load
{
scriptything.register("function increment(x) { $('#myLiteral').increment(); });
}
或其他东西,但这不会是 myControl.increment,而只是increment()。页面上有多个控件会搞砸。
有人知道我如何实现将 javascript 与自定义控件耦合的目标吗?我问的是不可能的事吗?
As a starting point for creating custom controls, I would like to make a control that simply displays a number. If we imagine the .ascx file contains nothing except for a literal control, and the code behind sets that value to 1.
I then want to be able to do myControl.increment();
This will run some javascript that increases the value of the literal control.
I could inject a javascript into the page, such as pseudocode:
page_load
{
scriptything.register("function increment(x) { $('#myLiteral').increment(); });
}
or something, but that wouldn't be myControl.increment, that would just be increment(). More than one control on the page would screw it up.
Anyone know how I can achieve my goal of coupling the javascript with the custom control? Am I asking the impossible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是很有可能的。
我有一个名为CustomControlLibrary 的类。
它包含几个文件夹,其中包括图像和脚本。 包含这些类型的项目
您可以使用
[assemply: WebResource]
IE然后在类的 onload 事件中,您可以注册客户端脚本,例如:
整个控件简单地放置在类中。它继承了WebControl和IPOstBackDataHandler。该控件特别适用于具有向左和向右箭头按钮并基于按住按钮来增加或减少文本框中的数值的控件。
It is very possible.
I have a class CalledCustomControlLibrary.
it contains several folders which include images and scripts. You can include those types of items using
[assemply: WebResource]
IE
Then in the onload event of the class you can register the client scripts like:
The entire control is housed simply in the class. which inherits WebControl and IPOstBackDataHandler. This control in particular is for a control that has a left and right arrow button and increments or decrements the number value in the text box based on holding the buttons.
是的,您想看看这样的 JavaScript 类开发作为一个示例(还有其他示例): http://www.howtocreate.co.uk/tutorials/javascript/objects
您可能真的想看看开发 ASP.NET AJAX 控件,它集成了服务器端控件和 AJAX 功能。我开发了一些,并写了一些关于这个主题的文章:
对于 JQuery,请使用:
HTH。
Yes, you want to look at JavaScript class development like this as one example (there are others): http://www.howtocreate.co.uk/tutorials/javascript/objects
You may really want to look at developing ASP.NET AJAX controls, which integrate server-side controls and AJAX capabilities. I developed a few and I have written a few articles on this subject:
For JQuery, use this:
HTH.