gpt4 book ai didi

ASP.NET 文本框 LostFocus 事件

转载 作者:行者123 更新时间:2023-12-02 09:30:20 27 4
gpt4 key购买 nike

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

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

最佳答案

我找到了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 文本框 LostFocus 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1310902/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com