Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 11th, 2006, 02:01 PM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default javascript getElementById returns null

Simple enough but it won't work:

I have an ASP.NET 2 VB page that inherits from a master page. The content page has an html button (not ASP:button) and an html label (not ASP:Label) in the content place holder area. The label's id attribute is: Label2. The reason the controls are html is so that I can manipulate them with a bit of javascript. But the javascript function for the button click does not instantiate an object. Here's the javascript.

function button1_onclick() {
    var x = document.getElementById('Label2');
    if (x != "")
        alert("Label2 is: " + x.value)
    else
        alert("Label2 has no value.")
}

At run time I get an error message saying that x is null.

Only possible thing I can think of is that I have installed Microsoft's Atlas and it has broken the getElementById method. But that's hard to believe.

Anyone?

Victor Victor


 
Old October 11th, 2006, 02:06 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

are you sure that the label is named label2 once you have the "compiled" page.. can you just see page code to see is the label has the name that you are looking for???

HTH

Gonzalo
 
Old October 11th, 2006, 02:36 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmmm. Well, logically speaking, doing x != "" is improper syntax, x is an object (for all intents in purposes) and can not = "" it can == null.

Try this:

var x = document.getElementById('Label2');
if(!x == null && x.value != ""){
//alert
}
else{
//alert
}

You are also missing ; after your alerts.

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 11th, 2006, 02:45 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

lol.. too much problems with the signature???

HTH

Gonzalo
 
Old October 11th, 2006, 02:47 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

OMG yes, everyone thinks I am being rude! =[
-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 11th, 2006, 02:54 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

maybe the rest of us can use a signature like

I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire Doug to do work for you.

:)

HTH

Gonzalo
 
Old October 11th, 2006, 03:02 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

LMFAO thats awesome! I like it. ;]

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 11th, 2006, 03:02 PM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks to all who responded. Here's an update.

1. Yes, the html label control's id is "Label2".

2. Javascript no longer requires a semicolor at the end of every statement. I generally do that out of habit but it is not necessary.

3. I tried the code from dparsons, still get the same result.

4. Have any of you actually gone thru the process of making a master page, then a content page derived from the master and adding a js file and calling it for the button's click event?

Thanks.

VV


 
Old October 11th, 2006, 03:05 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

In regards to #4, no I have not done this but what i have *heard* is that client side JS is a pain to get working with Master/Content pages. Sorry I can't be of more help.

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 11th, 2006, 03:08 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

About 4 I have js functions on a derived page, but i didn't try to put that on a .js file. if you put that function on the master page did it work??
if you put it on the derived page???

And of course, Doug can get paid.. :)

HTH

Gonzalo




Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript: null or not an object exception ganeshbraj ASP.NET 1.0 and 1.1 Professional 0 September 28th, 2007 08:01 AM
why File System GetAuditRules() returns null nomyjee ASP.NET 2.0 Professional 0 March 26th, 2006 08:06 PM
Using getElementById Frank Huddleston XML 4 November 11th, 2005 01:44 PM
getRealPath returns null in Fedora bbhutta Servlets 0 October 21st, 2005 07:20 AM
Access from a Dbase drop down returns NULL topshed Access 0 December 2nd, 2004 10:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.