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

October 11th, 2006, 02:01 PM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 11th, 2006, 02:06 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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
|
|

October 11th, 2006, 02:36 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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
|
|

October 11th, 2006, 02:45 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
lol.. too much problems with the signature???
HTH
Gonzalo
|
|

October 11th, 2006, 02:47 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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
|
|

October 11th, 2006, 02:54 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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
|
|

October 11th, 2006, 03:02 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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
|
|

October 11th, 2006, 03:02 PM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 11th, 2006, 03:05 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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
|
|

October 11th, 2006, 03:08 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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
|
|
 |