Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET TextBox LostFocus event
    primarykey
    data
    text
    <p>I need to trigger code on the server side to be called when a TextBox loses focus.</p> <p>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?</p> <p><b>Update:</b></p> <p>I have found a <a href="http://sbiefeld.com/2008/09/17/extending-the-aspnet-textbox-to-have-an-onblur-server-side-event/" rel="nofollow noreferrer">blog</a> 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.</p> <p>The following is my implementation in VB:</p> <pre><code>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('" &amp; UniqueID &amp; "','')") 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)" &amp; vbCrLf &amp; _ "{" &amp; vbCrLf &amp; _ " __doPostBack(control, arg);" &amp; vbCrLf &amp; _ "}" End Function Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent OnBlur() End Sub End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload