ASP.NET 文本框 LostFocus 事件
我需要在文本框失去焦点时触发服务器端的代码。
我知道有 onblur 客户端事件,并且没有 LostFocus 事件,那么当我的 TextBox 失去焦点时如何导致回发发生?
更新:
我发现了blog 这似乎为此提供了一个相当不错的解决方案。它涉及向 TextBox 子类添加自定义事件,并注册一个在 onblur JavaScript 客户端事件中调用服务器端事件的客户端脚本。
下面是我在VB中的实现:
Public Class MyTextBox
Inherits TextBox
Implements IPostBackEventHandler
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
MyBase.OnInit(e)
If Not Page.ClientScript.IsClientScriptBlockRegistered("OnBlurTextBoxEvent") Then
Page.ClientScript.RegisterStartupScript(MyBase.GetType, "OnBlurTextBoxEvent", GetScript, True)
Attributes.Add("onblur", "OnBlurred('" & UniqueID & "','')")
End If
End Sub
Public Delegate Sub OnBlurDelegate(ByVal sender As Object, ByVal e As EventArgs)
Public Event Blur As OnBlurDelegate
Protected Sub OnBlur()
RaiseEvent Blur(Me, EventArgs.Empty)
End Sub
Private Function GetScript() As String
Return "function OnBlurred(control, arg)" & vbCrLf & _
"{" & vbCrLf & _
" __doPostBack(control, arg);" & vbCrLf & _
"}"
End Function
Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
OnBlur()
End Sub
End Class
I need to trigger code on the server side to be called when a TextBox loses focus.
I know there is the onblur client side event, and that there is no LostFocus event, so how can I cause a postback to occur when my TextBox loses focus?
Update:
I have found a blog which seems to give a pretty decent solution to this. It involves adding a custom event to a TextBox subclass, and registering a client script which calls the server-side event in the onblur JavaScript client event.
The following is my implementation in VB:
Public Class MyTextBox
Inherits TextBox
Implements IPostBackEventHandler
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
MyBase.OnInit(e)
If Not Page.ClientScript.IsClientScriptBlockRegistered("OnBlurTextBoxEvent") Then
Page.ClientScript.RegisterStartupScript(MyBase.GetType, "OnBlurTextBoxEvent", GetScript, True)
Attributes.Add("onblur", "OnBlurred('" & UniqueID & "','')")
End If
End Sub
Public Delegate Sub OnBlurDelegate(ByVal sender As Object, ByVal e As EventArgs)
Public Event Blur As OnBlurDelegate
Protected Sub OnBlur()
RaiseEvent Blur(Me, EventArgs.Empty)
End Sub
Private Function GetScript() As String
Return "function OnBlurred(control, arg)" & vbCrLf & _
"{" & vbCrLf & _
" __doPostBack(control, arg);" & vbCrLf & _
"}"
End Function
Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
OnBlur()
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现了 博客 这似乎为此提供了一个相当不错的解决方案。它涉及向 TextBox 子类添加自定义事件,并注册一个在 onblur JavaScript 客户端事件中调用服务器端事件的客户端脚本。
下面是我在VB中的实现:
I have found a blog which seems to give a pretty decent solution to this. It involves adding a custom event to a TextBox subclass, and registering a client script which calls the server-side event in the onblur JavaScript client event.
The following is my implementation in VB:
谢谢你,它就像一个魅力。您只需更改一件事:将传递给 OnBlurred 函数的 UniqueID 值用引号引起来,因此它用作字符串而不是控件实例。即:
变为:
Thanks for that, it works like a charm. Just one thing you need to change: wrap the UniqueID value passed to the OnBlurred function in quotes, so it is used as a string and not the control instance. That is:
becomes:
嗯,这是一个相当奇怪的计划,但是您可以在客户端使用“onblur”来调用“form.submit();”。
Well, it's a fairly odd plan, but you can use 'onblur' on the client side to call 'form.submit();'.
为什么不使用 asp 文本框并将 AutoPostBack 属性设置为 true。
Why don't you use asp textbox with AutoPostBack property set to true.