1

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 :(

3
  • I have a feeling its because its not actually being included on the aspx page, but must it always be? Commented Mar 19, 2012 at 16:28
  • Yes, you still need to add Codebehind and Inherits in page header(aspx). Commented Mar 19, 2012 at 16:40
  • I know this sounds kind of basic - but if you view source, do you see your script in the code? Commented Mar 19, 2012 at 16:44

2 Answers 2

2

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" />
Sign up to request clarification or add additional context in comments.

1 Comment

I already tried that, with script tags or not it still doesnt work.
1

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

1 Comment

still doesn't work, I have tried with and without script tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.