简体   繁体   中英

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. I have created a simple test page to demonstrate my problem.

Here is the backend 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

<%@ 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.

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. 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.

Is this possible? I dont see where I am going wrong.

Please help :(

You probably have a javascript error sourcing from the line:

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. Either change this boolean value to False or remove the script tags ( see doc ):

Option a:

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

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

Option 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. Add the following in your form:

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

Try This:

The last parameter of RegisterClientScriptBlock should be false .

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

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" .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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