Referencing asp:Label from a javascript function
-
Thank you Sandepp for your reply. But it doesn't work. I can't get the label. I'm sure i'm using its correct id and name. I checked the rendered html. Here's the code putting the label: calling it from a javascript function: var labelErr = document.getElementByName("lblError"); if(labelErr) { window.alret("wow"); labelErr.value = "New Value mother fucker!"; } else { window.alert("now"); }
What problem are you getting (post the error) ? And What you want to do with labelErr ?
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
-
What problem are you getting (post the error) ? And What you want to do with labelErr ?
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
window.alert("Hi 1"); var labelErr = document.getElementByName("lblError"); window.alert("Hi 2"); labelErr.value = "There is an error"; I want to put new text in the label. There's no error, only nothing happens with the label. I will get the first message box with "Hi 1" but i will not get the second. Meaning, there's a problem with the second line of code.
-
You can't access a server control from Javascript, as the server control only exists on the server while the page is created. Examine the source of the web page to find out how the control is rendered into html elements, which you can access from Javascript.
--- single minded; short sighted; long gone;
-
window.alert("Hi 1"); var labelErr = document.getElementByName("lblError"); window.alert("Hi 2"); labelErr.value = "There is an error"; I want to put new text in the label. There's no error, only nothing happens with the label. I will get the first message box with "Hi 1" but i will not get the second. Meaning, there's a problem with the second line of code.
snir_ya wrote:
var labelErr = document.getElementByName("lblError");
it should be document.getElementById("lblError"); instead of name as you have not given name try with that
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
-
Of course i did Guffa. This code will not pop up the second alert only the first one. I'm sure the label's html name is "lblError". window.alert("Hi 1"); var labelErr = document.getElementByName("lblError"); window.alert("Hi 2");
snir_ya wrote:
Of course i did Guffa.
Yes but Guffa is right as you are not getting the asp control but its html source as the server control are not active in client side they rendered at server side
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
-
snir_ya wrote:
var labelErr = document.getElementByName("lblError");
it should be document.getElementById("lblError"); instead of name as you have not given name try with that
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
Yessssss! it worked. Thanks SunDeep. Now, when i try to set text to this label it doesn't have any effect: var labelErr = document.getElementById("lblError"); labelErr.text = "Hopps"; or var labelErr = document.getElementById("lblError"); labelErr.value = "Hopps"; Do you know why?
-
Hi, I'm trying to reference a server control asp:label from a javascript function during a client side validation procedure. What is the right way to reference an already-rendered server control? Thanks. Here's my javascript code:
<!-- function ValidateBppText(sender, args) { var bppval = parseInt(args.Value); if(bppVal>=25 && bppVal<=350) { args.IsValid = true; } else { <b>document.forms[0].lblError.value </b>= "BPP IS OUT OF RANGE!"; args.IsValid = false; } //-->
this is the way to do
var f=document.getElementById("Label1") alert(f.innerText) f.innerText = 'sdfsdf'
-- modified at 10:39 Thursday 15th March, 2007Regards, Sylvester G sylvester_g_m@yahoo.com
-
Yessssss! it worked. Thanks SunDeep. Now, when i try to set text to this label it doesn't have any effect: var labelErr = document.getElementById("lblError"); labelErr.text = "Hopps"; or var labelErr = document.getElementById("lblError"); labelErr.value = "Hopps"; Do you know why?
lable Don't have the value its innertext labelErr.innerText="Some text";
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
-
lable Don't have the value its innertext labelErr.innerText="Some text";
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
-
Thanks a lot Sandeep. After three days it finally works. How did you know to use innerText? Do you know of a good html reference (web site or tutorial) that summerizes html elements attributes? Snir.
My pleasure :) Don't know but you can google it ? There are lots of resources on the web you will definetly get it :rolleyes:
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
-
this is the way to do
var f=document.getElementById("Label1") alert(f.innerText) f.innerText = 'sdfsdf'
-- modified at 10:39 Thursday 15th March, 2007Regards, Sylvester G sylvester_g_m@yahoo.com
-
Of course i did Guffa. This code will not pop up the second alert only the first one. I'm sure the label's html name is "lblError". window.alert("Hi 1"); var labelErr = document.getElementByName("lblError"); window.alert("Hi 2");
That's because the document object doesn't have any method named getElementByName. More than one element can have the same name, so the method is named getElementsByName and returns a collection of elements. If you want to access a single element, use the getElementById method.
--- single minded; short sighted; long gone;
-
Hey, thanks a lot. How was i supposed to know to use innerText? Is innerText an attribute of span or label? Do you know of a reference that summerizes all of this? Sorry for dropping this on you mate, but you've just shown me the light...:-D
Use innerHTML instead of innerText, it's more widely supported.
snir_ya wrote:
Is innerText an attribute of span or label?
It's an attribute of the span element. The Label control does not exist in the browser, only on the server.
snir_ya wrote:
Do you know of a reference that summerizes all of this?
MSDN Library[^] covers most of it. As the html documentation is really documentation over Internet Explorer, you have to watch the standards information. It often says "There is no public standard that applies to this ...", which means that it probably is not supported in any other browser. W3C[^] has the actual standards.
--- single minded; short sighted; long gone;