【问题标题】:ASP.NET TextBox LostFocus eventASP.NET TextBox LostFocus 事件
【发布时间】:2009-08-21 09:18:40
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      变成:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 我没有尝试这个,因为我需要知道什么控制失去了焦点
        【解决方案4】:

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

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

        【讨论】:

        • 即使 AutoPostBack 设置为 true,TextBox 上仍然没有 LostFocus 事件
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多