Pat*_*ald 5 asp.net textbox lost-focus custom-event
当TextBox失去焦点时,我需要在服务器端触发代码.
我知道有是的onblur客户端事件,并没有LostFocus事件,所以我怎么可能会导致当我的文本框失去焦点发生回发?
更新:
我找到了一个博客,似乎给出了相当不错的解决方案.它涉及向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
Run Code Online (Sandbox Code Playgroud)
我发现一个博客似乎对此提供了一个相当不错的解决方案。它涉及向 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42370 次 |
| 最近记录: |