我需要在服务器端触发代码,以便在TextBox失去焦点时被调用。ASP.NET TextBox 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
我没有尝试这个,因为我需要知道什么控制失去焦点 – 2009-08-21 13:15:17