用于文本框的asp.net自定义onfocus事件

时间:2022-08-25 17:35:15

Is it possible to create a custom onfocus server side event for a textbox control?

是否可以为文本框控件创建自定义onfocus服务器端事件?

I would like to simply fire a server side method when a textbox has focus.

我想简单地在文本框具有焦点时触发服务器端方法。

5 个解决方案

#1


There's nothing built-in, because focusing a textbox doesn't cause the page to submit itself. You could always attach an event to the focus on the client side, and then submit some AJAX request to the server, but I'm curious to know what exactly you're trying to achieve here.

没有任何内置功能,因为聚焦文本框不会导致页面自行提交。您可以随时将事件附加到客户端的焦点上,然后向服务器提交一些AJAX请求,但我很想知道您在这里想要实现的目标。

#2


I would strongly recommend using client side validation for this. You can use the ASP.NET Validators for this. One of the properties on the validator is to set focus on error. This should be more than enough for what you are trying to do and it also limits the number of server side roundtrips.

我强烈建议使用客户端验证。您可以使用ASP.NET验证器。验证器的一个属性是将注意力集中在错误上。这应该足以满足您的要求,并且还限制了服务器端往返的数量。

If you're really feeling creative, you can look at some of the other validation options and AJAX Control Panel extenders. There are some pretty snazzy validator controls out there. Another option would be to consider using some jQuery validation.

如果您真的有创意,可以查看其他一些验证选项和AJAX控制面板扩展器。那里有一些非常时髦的验证器控件。另一种选择是考虑使用一些jQuery验证。

You should still validate on the server, but what you are describing sounds like a perfect it for client validation first.

您仍然应该在服务器上进行验证,但是您所描述的内容听起来像是一个完美的客户端验证。

#3


Ok, i got a web form and on submission validation takes place, if there are errors on the page then these errors get stored in state and a warning sign gets displayed.

好的,我有一个Web表单并且在提交验证时发生,如果页面上有错误,那么这些错误将被存储在状态中并显示警告标志。

Now, when the user goes through the errors, i wanted a popup box to be displayed by the side of the textbox they are currently correcting.

现在,当用户完成错误时,我希望弹出框显示在他们当前正在纠正的文本框的一侧。

for this to happen i wanted to have an event attached to an onfocus type handle.

为此,我希望将一个事件附加到onfocus类型的句柄上。

#4


Is a server-side onFocus really the right solution for that? If you want validation popups and such, you're probably better off with client-side solution. Having a form submit when you simply tab to a textbox would not be good usability.

服务器端的onFocus真的是正确的解决方案吗?如果你想要验证弹出窗口等,你可能会更好地使用客户端解决方案。当您只是选中文本框时提交表单将不是一个好的可用性。

#5


You could do this using a custom validator hooked up to some AJAX call, however you only need to do a server trip if there is data on the server that you current data needs to be validated against.

您可以使用连接到某些AJAX调用的自定义验证器来执行此操作,但是如果服务器上存在您需要验证当前数据的数据,则只需要执行服务器跳转。

#1


There's nothing built-in, because focusing a textbox doesn't cause the page to submit itself. You could always attach an event to the focus on the client side, and then submit some AJAX request to the server, but I'm curious to know what exactly you're trying to achieve here.

没有任何内置功能,因为聚焦文本框不会导致页面自行提交。您可以随时将事件附加到客户端的焦点上,然后向服务器提交一些AJAX请求,但我很想知道您在这里想要实现的目标。

#2


I would strongly recommend using client side validation for this. You can use the ASP.NET Validators for this. One of the properties on the validator is to set focus on error. This should be more than enough for what you are trying to do and it also limits the number of server side roundtrips.

我强烈建议使用客户端验证。您可以使用ASP.NET验证器。验证器的一个属性是将注意力集中在错误上。这应该足以满足您的要求,并且还限制了服务器端往返的数量。

If you're really feeling creative, you can look at some of the other validation options and AJAX Control Panel extenders. There are some pretty snazzy validator controls out there. Another option would be to consider using some jQuery validation.

如果您真的有创意,可以查看其他一些验证选项和AJAX控制面板扩展器。那里有一些非常时髦的验证器控件。另一种选择是考虑使用一些jQuery验证。

You should still validate on the server, but what you are describing sounds like a perfect it for client validation first.

您仍然应该在服务器上进行验证,但是您所描述的内容听起来像是一个完美的客户端验证。

#3


Ok, i got a web form and on submission validation takes place, if there are errors on the page then these errors get stored in state and a warning sign gets displayed.

好的,我有一个Web表单并且在提交验证时发生,如果页面上有错误,那么这些错误将被存储在状态中并显示警告标志。

Now, when the user goes through the errors, i wanted a popup box to be displayed by the side of the textbox they are currently correcting.

现在,当用户完成错误时,我希望弹出框显示在他们当前正在纠正的文本框的一侧。

for this to happen i wanted to have an event attached to an onfocus type handle.

为此,我希望将一个事件附加到onfocus类型的句柄上。

#4


Is a server-side onFocus really the right solution for that? If you want validation popups and such, you're probably better off with client-side solution. Having a form submit when you simply tab to a textbox would not be good usability.

服务器端的onFocus真的是正确的解决方案吗?如果你想要验证弹出窗口等,你可能会更好地使用客户端解决方案。当您只是选中文本框时提交表单将不是一个好的可用性。

#5


You could do this using a custom validator hooked up to some AJAX call, however you only need to do a server trip if there is data on the server that you current data needs to be validated against.

您可以使用连接到某些AJAX调用的自定义验证器来执行此操作,但是如果服务器上存在您需要验证当前数据的数据,则只需要执行服务器跳转。