在 asp.net 中创建具有 javascript 属性和方法的自定义控件

发布于 2024-10-19 00:55:43 字数 436 浏览 1 评论 0原文

作为创建自定义控件的起点,我想制作一个仅显示数字的控件。如果我们想象 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法言说的痛 2024-10-26 00:55:43

这是很有可能的。

我有一个名为CustomControlLibrary 的类。

它包含几个文件夹,其中包括图像和脚本。 包含这些类型的项目

您可以使用 [assemply: WebResource] IE

[assembly: WebResource("CustomControlLibrary.LeftRight.local_img.leftArrow.jpg", "img/jpg")]
[assembly: WebResource("CustomControlLibrary.LeftRight.local_img.rightArrow.jpg", "img/jpg")]
[assembly: WebResource("CustomControlLibrary.LeftRight.local_js.leftright.js", "application/javascript")]
[assembly: WebResource("CustomControlLibrary.global_js.jquery-1.3.2.js", "application/javascript")]
[assembly: WebResource("CustomControlLibrary.global_js.mousehold.js", "application/javascript")]

然后在类的 onload 事件中,您可以注册客户端脚本,例如:

        string tsJQueryJS = Page.ClientScript.GetWebResourceUrl(this.GetType(), "CustomControlLibrary.global_js.jquery-1.3.2.js") ;
        string tsMousehold = Page.ClientScript.GetWebResourceUrl(this.GetType(), "CustomControlLibrary.global_js.mousehold.js");
        string tsLeftRightJS = Page.ClientScript.GetWebResourceUrl(this.GetType(), "CustomControlLibrary.LeftRight.local_js.leftright.js");

        if (!Page.ClientScript.IsClientScriptIncludeRegistered("JQuery" )) { Page.ClientScript.RegisterClientScriptInclude("JQuery", tsJQueryJS); }
        if (!Page.ClientScript.IsClientScriptIncludeRegistered("Mousehold")) { Page.ClientScript.RegisterClientScriptInclude("Mousehold", tsMousehold); }
        if (!Page.ClientScript.IsClientScriptIncludeRegistered("LefRightJS")) { Page.ClientScript.RegisterClientScriptInclude("LeftRightJS", tsLeftRightJS); }

整个控件简单地放置在类中。它继承了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

[assembly: WebResource("CustomControlLibrary.LeftRight.local_img.leftArrow.jpg", "img/jpg")]
[assembly: WebResource("CustomControlLibrary.LeftRight.local_img.rightArrow.jpg", "img/jpg")]
[assembly: WebResource("CustomControlLibrary.LeftRight.local_js.leftright.js", "application/javascript")]
[assembly: WebResource("CustomControlLibrary.global_js.jquery-1.3.2.js", "application/javascript")]
[assembly: WebResource("CustomControlLibrary.global_js.mousehold.js", "application/javascript")]

Then in the onload event of the class you can register the client scripts like:

        string tsJQueryJS = Page.ClientScript.GetWebResourceUrl(this.GetType(), "CustomControlLibrary.global_js.jquery-1.3.2.js") ;
        string tsMousehold = Page.ClientScript.GetWebResourceUrl(this.GetType(), "CustomControlLibrary.global_js.mousehold.js");
        string tsLeftRightJS = Page.ClientScript.GetWebResourceUrl(this.GetType(), "CustomControlLibrary.LeftRight.local_js.leftright.js");

        if (!Page.ClientScript.IsClientScriptIncludeRegistered("JQuery" )) { Page.ClientScript.RegisterClientScriptInclude("JQuery", tsJQueryJS); }
        if (!Page.ClientScript.IsClientScriptIncludeRegistered("Mousehold")) { Page.ClientScript.RegisterClientScriptInclude("Mousehold", tsMousehold); }
        if (!Page.ClientScript.IsClientScriptIncludeRegistered("LefRightJS")) { Page.ClientScript.RegisterClientScriptInclude("LeftRightJS", tsLeftRightJS); }

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.

君勿笑 2024-10-26 00:55:43

是的,您想看看这样的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文