Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Referencing asp:Label from a javascript function

Referencing asp:Label from a javascript function

Scheduled Pinned Locked Moved ASP.NET
17 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S snir_ya

    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"); }

    S Offline
    S Offline
    Sandeep Akhare
    wrote on last edited by
    #5

    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!

    S 1 Reply Last reply
    0
    • S Sandeep Akhare

      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!

      S Offline
      S Offline
      snir_ya
      wrote on last edited by
      #6

      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.

      S 1 Reply Last reply
      0
      • G Guffa

        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;

        S Offline
        S Offline
        snir_ya
        wrote on last edited by
        #7

        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");

        S G 2 Replies Last reply
        0
        • S snir_ya

          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.

          S Offline
          S Offline
          Sandeep Akhare
          wrote on last edited by
          #8

          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!

          S 1 Reply Last reply
          0
          • S snir_ya

            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");

            S Offline
            S Offline
            Sandeep Akhare
            wrote on last edited by
            #9

            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!

            1 Reply Last reply
            0
            • S Sandeep Akhare

              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!

              S Offline
              S Offline
              snir_ya
              wrote on last edited by
              #10

              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?

              S 1 Reply Last reply
              0
              • S snir_ya

                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; } //-->

                S Offline
                S Offline
                Sylvester george
                wrote on last edited by
                #11

                this is the way to do var f=document.getElementById("Label1") alert(f.innerText) f.innerText = 'sdfsdf' -- modified at 10:39 Thursday 15th March, 2007

                Regards, Sylvester G sylvester_g_m@yahoo.com

                S 1 Reply Last reply
                0
                • S snir_ya

                  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?

                  S Offline
                  S Offline
                  Sandeep Akhare
                  wrote on last edited by
                  #12

                  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!

                  S 1 Reply Last reply
                  0
                  • S Sandeep Akhare

                    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!

                    S Offline
                    S Offline
                    snir_ya
                    wrote on last edited by
                    #13

                    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.

                    S 1 Reply Last reply
                    0
                    • S snir_ya

                      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.

                      S Offline
                      S Offline
                      Sandeep Akhare
                      wrote on last edited by
                      #14

                      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!

                      1 Reply Last reply
                      0
                      • S Sylvester george

                        this is the way to do var f=document.getElementById("Label1") alert(f.innerText) f.innerText = 'sdfsdf' -- modified at 10:39 Thursday 15th March, 2007

                        Regards, Sylvester G sylvester_g_m@yahoo.com

                        S Offline
                        S Offline
                        snir_ya
                        wrote on last edited by
                        #15

                        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

                        G 1 Reply Last reply
                        0
                        • S snir_ya

                          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");

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #16

                          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;

                          1 Reply Last reply
                          0
                          • S snir_ya

                            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

                            G Offline
                            G Offline
                            Guffa
                            wrote on last edited by
                            #17

                            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;

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups