ASP.NET 文本框 LostFocus 事件

发布于 2024-08-02 16:52:26 字数 1537 浏览 6 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

楠木可依 2024-08-09 16:52:26

我发现了 博客 这似乎为此提供了一个相当不错的解决方案。它涉及向 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

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
歌入人心 2024-08-09 16:52:26

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

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

变为:

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

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 & "','')") 
吃不饱 2024-08-09 16:52:26

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

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

远山浅 2024-08-09 16:52:26

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

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

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

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文