简体   繁体   English

ASP.NET Javascript Textbox onblur

[英]ASP.NET Javascript Textbox onblur

I'm having some problems getting a javascript registered on the page so my textbox can fire it on onBlur. 我在页面上注册javascript时遇到了一些问题,所以我的文本框可以在onBlur上启动它。 I have created a simple test page to demonstrate my problem. 我创建了一个简单的测试页面来演示我的问题。

Here is the backend VB.Net 这是后端VB.Net

Public Class Test
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim scriptText As String = ""
    scriptText &= "<script language='javascript'>"
    scriptText &= "function DisplayBlurMessage(){alert('you clicked outside the textbox');}"
    scriptText &= "</script>"

    ClientScript.RegisterClientScriptBlock(Me.GetType(), _
       "BlurScript", scriptText, False)

End Sub
End Class

Here is the FrontEnd .aspx file 这是FrontEnd .aspx文件

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Test.aspx.vb" Inherits="WebApplication1.Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Test Page</title>

</head>
<body>
<h3>Test Page</h3>
<form id="form1" runat="server">

<asp:TextBox id="TextBox1" columns="54" 
 Text="Click here then outside" 
 runat="server" onBlur="DisplayBlurMessage();"/>  

</form>
</body>
</html>

When i view the page, clicking out of the textbox, the javascript debugger gives me an error because the javascript isn't defined in the source. 当我查看页面时,单击文本框,javascript调试器给我一个错误,因为源中没有定义javascript。

I can however get it to work by putting the Page_Load sub in tags in the aspx file and then accessing the Attributes of the textbox directly. 然而,我可以通过将Page_Load子标记放在aspx文件中的标记然后直接访问文本框的属性来使其工作。 But this is not what I want. 但这不是我想要的。

Basically for my final page, I am going to want to iterate through all the textbox's on the page and then give them all a onBlur and onFocus methods that use their id's. 基本上对于我的最后一页,我想要遍历页面上的所有文本框,然后给它们所有使用它们的id的onBlur和onFocus方法。

Is this possible? 这可能吗? I dont see where I am going wrong. 我不明白我哪里错了。

Please help :( 请帮忙 :(

You probably have a javascript error sourcing from the line: 您可能从该行获取javascript错误:

ClientScript.RegisterClientScriptBlock(Me.GetType(), _
       "BlurScript", scriptText, True)

The True means that the scriptText should be wrapped with script tags but you already have included the script tags. True表示scriptText应该包含脚本标记,但您已经包含了脚本标记。 Either change this boolean value to False or remove the script tags ( see doc ): 将此布尔值更改为False或删除脚本标记( 请参阅doc ):

Option a: 选项a:

Dim scriptText As String = ""
scriptText &= "function DisplayBlurMessage(){alert('you clicked outside the textbox');}"

ClientScript.RegisterClientScriptBlock(Me.GetType(), _
   "BlurScript", scriptText, True)

Option b: 选项b:

Dim scriptText As String = ""
scriptText &= "<script language='javascript'>"
scriptText &= "function DisplayBlurMessage(){alert('you clicked outside the textbox');}"
scriptText &= "</script>"

ClientScript.RegisterClientScriptBlock(Me.GetType(), _
   "BlurScript", scriptText, False)

Edit: 编辑:

In order to register a script you need to have a ScriptManager in your page. 要注册脚本,您需要在页面中使用ScriptManager。 Add the following in your form: 在表单中添加以下内容:

<asp:ScriptManager ID="ScriptManager1" runat="server" />

Try This: 尝试这个:

The last parameter of RegisterClientScriptBlock should be false . RegisterClientScriptBlock的最后一个参数应为false

  ClientScript.RegisterClientScriptBlock(Me.GetType(), _
       "BlurScript", scriptText, False)

http://msdn.microsoft.com/en-us/library/bahh2fef.aspx http://msdn.microsoft.com/en-us/library/bahh2fef.aspx

EDIT: In Page header, you also need to put CodeBehind="Test.aspx.vb" and Inherits="yourproject.Test" . 编辑:在页眉中,您还需要将CodeBehind="Test.aspx.vb"Inherits="yourproject.Test"

<%@ Page Language="VB" AutoEventWireup="True" CodeBehind="Test.aspx.vb" Inherits="yourproject.Test" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM