ASP.NET TextBox LostFocus 事件

16

本文介绍了ASP.NET TextBox LostFocus 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I need to trigger code on the server side to be called when a TextBox loses focus.

我知道有 onblur 客户端事件,并且没有 LostFocus 事件,那么当我的 TextBox 失去焦点时如何导致回发?

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?

更新:

我找到了一个 blog 似乎为此提供了一个相当不错的解决方案.它涉及向 TextBox 子类添加自定义事件,并在 onblur JavaScript 客户端事件中注册调用服务器端事件的客户端脚本.

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.

以下是我在VB中的实现:

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

推荐答案

我找到了一个blog 似乎为此提供了一个相当不错的解决方案.它涉及向 TextBox 子类添加自定义事件,并在 onblur JavaScript 客户端事件中注册调用服务器端事件的客户端脚本.

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.

以下是我在VB中的实现:

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

这篇关于ASP.NET TextBox LostFocus 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

用 C# 编写全局自定义事件
Writing a global custom event in C#(用 C# 编写全局自定义事件)...
2023-11-11 C#/.NET开发问题
2

是否可以将委托作为属性参数?
Is it possible to have a delegate as attribute parameter?(是否可以将委托作为属性参数?)...
2023-11-11 C#/.NET开发问题
0

请求的性能计数器不是自定义计数器,它必须初始化为 ReadOnly
The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly(请求的性能计数器不是自定义计数器,它必须初始化为 ReadOnly)...
2023-11-11 C#/.NET开发问题
11

Func<string,string> 有什么区别?和委托?
What is the difference between Funclt;string,stringgt; and delegate?(Funclt;string,stringgt; 有什么区别?和委托?)...
2023-11-11 C#/.NET开发问题
3

如何通过串口 RS-232 或 USB 转换器将体重秤的重量显示到文本框中?
How to display weight from weighing scale into a textbox via serial port RS-232 or usb converter?(如何通过串口 RS-232 或 USB 转换器将体重秤的重量显示到文本框中?)...
2023-11-10 C#/.NET开发问题
5

C# 语法通过数组中的构造函数参数初始化自定义类/对象?
C# syntax to initialize custom class/objects through constructor params in array?(C# 语法通过数组中的构造函数参数初始化自定义类/对象?)...
2023-11-10 C#/.NET开发问题
2