我需要在服務器端觸發代碼,以便在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