服务器或客户端自定义ASP.Net控件?

时间:2022-02-19 15:25:35

I am an undergraduate student , and working on my Final Year project these days. I have some queries related to Custom Controls as follows: I am designing a text box field which will have three or more functions as follows :

我是一名本科生,现在正在为我的最后一年项目工作。我有一些与自定义控件相关的查询,如下所示:我正在设计一个文本框字段,它将具有三个或更多功能,如下所示:

  • Either it will allow numeric characters only
  • 它只允许使用数字字符
  • Or it will allow an email address to be taken as input
  • 或者它将允许将电子邮件地址作为输入
  • Or it will be a file Upload text box
  • 或者它将是一个文件上传文本框

I am using jQuery to validate this text box .for eg. for checking whether the user has entered numeric characters only or not!!

我正在使用jQuery来验证此文本框。例如。用于检查用户是否仅输入了数字字符!!

My Question is

我的问题是

  • What is the better approach to build such custom controls ? Either make it pure client side or pure Server side or both?
  • 构建此类自定义控件的更好方法是什么?要么是纯客户端还是纯服务器端,要么两者兼而有之?
  • Also , I need to include AJAX functionality in file uploader. If the client browser doesnot support JavaScripting for some reason then how we can avoid this constraint ? Thank you very much for your time ! Kindly help me.
  • 另外,我需要在文件上传器中包含AJAX功能。如果客户端浏览器由于某种原因不支持JavaScripting,那么我们如何避免这种约束呢?非常感谢您的宝贵时间 !请帮助我。

1 个解决方案

#1


0  

  1. First of all you have to decide if you need both client-side and server-side functionality for your control. It will depend on your needs. If you are writing it as part of a large application, I would suggest going for both, because it's much easier to manage. If you decide that you do want both, ASP.NET includes exact functionality that you are looking for. It's called Extender controls. They will allow you to create a custom server-side control and extend that control to include some client-side functionality. You can get more information about Extenders here.

    首先,您必须确定是否需要控件的客户端和服务器端功能。这取决于您的需求。如果您将其作为大型应用程序的一部分编写,我建议您同时使用它们,因为它更易于管理。如果您确定两者都需要,ASP.NET将包含您正在寻找的确切功能。它被称为Extender控件。它们将允许您创建自定义服务器端控件并扩展该控件以包含一些客户端功能。您可以在此处获取有关Extenders的更多信息。

  2. Graceful failing AJAX controls are rare, majority of the developers that create AJAX controls assume that all clients will have JavaScript turned on. However, they are not that hard to do. Actually they are very easy to do, if you are using ASP.NET AJAX Update Panel. Update Panel itself will automatically switch to full post-back if JavaScripts are disabled. If you are using custom implementation of AJAX, or jQuery (as you mentioned above), you have to follow a few simple rules. First of all, avoid binding events from inside scripts, use onclick, onmouseover, etc. This way, if a link has onclick event, and a valid href tag, if JavaScript are on, you will process onclick event handler, but if they are off, you will just follow href attribute value. For the uploader, you can put your uploader inside the FORM element, and add onsubmit event to it. If JS is on, you will process onsubmit, and do an AJAX call to save the file, if JS is off, you will do a full page post-back and save the file from the server-side.

    优秀的失败AJAX控件很少见,大多数创建AJAX控件的开发人员都假设所有客户端都启用了JavaScript。但是,它们并不难做到。实际上,如果您使用的是ASP.NET AJAX Update Panel,它们很容易实现。如果禁用JavaScripts,更新面板本身将自动切换到完全回发。如果您使用的是AJAX或jQuery的自定义实现(如上所述),则必须遵循一些简单的规则。首先,避免从脚本内部绑定事件,使用onclick,onmouseover等。这样,如果链接有onclick事件和有效的href标记,如果JavaScript打开,你将处理onclick事件处理程序,但如果它们是关闭,您将只关注href属性值。对于上传器,您可以将上传器放在FORM元素中,并向其添加onsubmit事件。如果JS处于打开状态,您将处理onsubmit,并执行AJAX调用以保存文件,如果JS关闭,您将回发整页并从服务器端保存文件。

#1


0  

  1. First of all you have to decide if you need both client-side and server-side functionality for your control. It will depend on your needs. If you are writing it as part of a large application, I would suggest going for both, because it's much easier to manage. If you decide that you do want both, ASP.NET includes exact functionality that you are looking for. It's called Extender controls. They will allow you to create a custom server-side control and extend that control to include some client-side functionality. You can get more information about Extenders here.

    首先,您必须确定是否需要控件的客户端和服务器端功能。这取决于您的需求。如果您将其作为大型应用程序的一部分编写,我建议您同时使用它们,因为它更易于管理。如果您确定两者都需要,ASP.NET将包含您正在寻找的确切功能。它被称为Extender控件。它们将允许您创建自定义服务器端控件并扩展该控件以包含一些客户端功能。您可以在此处获取有关Extenders的更多信息。

  2. Graceful failing AJAX controls are rare, majority of the developers that create AJAX controls assume that all clients will have JavaScript turned on. However, they are not that hard to do. Actually they are very easy to do, if you are using ASP.NET AJAX Update Panel. Update Panel itself will automatically switch to full post-back if JavaScripts are disabled. If you are using custom implementation of AJAX, or jQuery (as you mentioned above), you have to follow a few simple rules. First of all, avoid binding events from inside scripts, use onclick, onmouseover, etc. This way, if a link has onclick event, and a valid href tag, if JavaScript are on, you will process onclick event handler, but if they are off, you will just follow href attribute value. For the uploader, you can put your uploader inside the FORM element, and add onsubmit event to it. If JS is on, you will process onsubmit, and do an AJAX call to save the file, if JS is off, you will do a full page post-back and save the file from the server-side.

    优秀的失败AJAX控件很少见,大多数创建AJAX控件的开发人员都假设所有客户端都启用了JavaScript。但是,它们并不难做到。实际上,如果您使用的是ASP.NET AJAX Update Panel,它们很容易实现。如果禁用JavaScripts,更新面板本身将自动切换到完全回发。如果您使用的是AJAX或jQuery的自定义实现(如上所述),则必须遵循一些简单的规则。首先,避免从脚本内部绑定事件,使用onclick,onmouseover等。这样,如果链接有onclick事件和有效的href标记,如果JavaScript打开,你将处理onclick事件处理程序,但如果它们是关闭,您将只关注href属性值。对于上传器,您可以将上传器放在FORM元素中,并向其添加onsubmit事件。如果JS处于打开状态,您将处理onsubmit,并执行AJAX调用以保存文件,如果JS关闭,您将回发整页并从服务器端保存文件。