views:

4295

answers:

5

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
A: 

Well, it's a fairly odd plan, but you can use 'onblur' on the client side to call 'form.submit();'.

Noon Silk
I didn't try this as I need to know what control has lost focus
Patrick McDonald
A: 

Why don't you use asp textbox with AutoPostBack property set to true.

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
rahul
Even with AutoPostBack set to true, there is still no LostFocus event on the TextBox
Patrick McDonald
A: 

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
Patrick McDonald
A: 

I am trying for validating a textbox

I tried this code, but couldn't success. Can u pl. give the client side code of this (vb.net)

Thanks

Santhosh
+1  A: 

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:

Attributes.Add("onblur", "OnBlurred(" & UniqueID & ",'')") 

becomes:

Attributes.Add("onblur", "OnBlurred('" & UniqueID & "','')") 
Pj
Thanks for that, I've updated my answer with your comment
Patrick McDonald