ASP.NET TextBox LostFocus事件

时间:2009-08-21 09:18:41

标签: asp.net textbox lost-focus custom-event

当TextBox失去焦点时,我需要在服务器端触发代码。

我知道有一个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

4 个答案:

答案 0 :(得分:3)

我找到了一个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

答案 1 :(得分:2)

谢谢你,它就像一个魅力。只需要更改一件事:将引用的OnBlurred函数传递给OnBlurred函数,因此它用作字符串而不是控件实例。那就是:

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

变为:

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

答案 2 :(得分:0)

嗯,这是一个相当奇怪的计划,但您可以在客户端使用'onblur'来调用'form.submit();'。

答案 3 :(得分:-1)

为什么不使用 AutoPostBack 属性设置为true的asp文本框。

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>