2009-08-21 69 views
5

我需要在服务器端触发代码,以便在TextBox失去焦点时被调用。ASP.NET TextBox LostFocus事件

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

回答

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 
0

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

+0

我没有尝试这个,因为我需要知道什么控制失去焦点 – 2009-08-21 13:15:17

-1

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

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

即使的AutoPostBack设置为true,仍然在没有LostFocus事件TextBox – 2009-08-21 13:14:43

2

感谢的是,它的工作原理就像一个魅力。只需要改变一件事:将传递给OnBlurred函数的UniqueID值用引号括起来,所以它用作字符串而不是控件实例。那就是:

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

变为:

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

感谢您的评论,我更新了我的答案 – 2010-07-12 12:23:25