Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET TextBox LostFocus event

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
like image 491
Patrick McDonald Avatar asked Oct 15 '22 14:10

Patrick McDonald


People also ask

What is WPF LostFocus?

The LostFocus event occurs when the specified object loses the focus.

What is LostFocus event?

The LostFocus event occurs after the Exit event. If you move the focus to a control on a form, and that control doesn't have the focus on that form, the Exit and LostFocus events for the control that does have the focus on the form occur before the Enter and GotFocus events for the control you moved to.

How to get Lost focus event of textbox in asp net?

lost focus of a textbox is a client side event. What you can do is call the server side code using AJAX request in onblur method of the textbox. Or simply use TextChanged server side event. It will actually get called once you lost focus from the text box.


1 Answers

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
like image 147
Patrick McDonald Avatar answered Oct 18 '22 19:10

Patrick McDonald