Solved

Accessing/Setting Text inside a Label Control on an ASP.net Page (C#)

Posted on 2009-07-14
5
387 Views
Last Modified: 2012-06-27
Good Afternoon,
I have been racking my brain on how to access/set the text inside a ASP.NET Label Control with client side javascript.
...And yes i have search this site for answers (for about 2 hours)
I have tried mutliple answers posted here and the closest I can get is 'undefined' to appear in the popup alert i have been using to test the response .
I am looking to grab the results of HTML radiobutton generated dynamically (works like a dream) and store the value a Label Control.
I am also trying to access the text in the lblNumberOfUsers control to let the function check more than one set of radio buttons.

Help...pulling what hair i have left out...lol

function getCheckedValue(objName) {

        

        //var numberOfGuest = document.getElementById('lblNumberOfUsers').text;

        var numberOfGuest = document.getElementsByName('lblNumberOfUsers').innerHTML;

        //var numberOfGuest = document.getElementById('lblNumberOfUsers').innerHTML;

        alert(numberOfGuest);
 

        var radioObj = document.getElementsByName(objName);

        if (!radioObj)

            return "";

        var radioLength = radioObj.length;

        if (radioLength == undefined)

            if (radioObj.checked)

            return radioObj.value;

        else

            return "";

        for (var i = 0; i < radioLength; i++) {

            if (radioObj[i].checked) {

                //return radioObj[i].value;

                alert(radioObj[i].value);

            }

        }

        return "";

Open in new window

0
Comment
Question by:jvancojr
  • 2
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
mohan_sekar earned 250 total points
ID: 24852506
this will do

var numberOfGuest = document.getElementsById('<%=lblNumberOfUsers.ClientId%>').innerHTML;
0
 
LVL 8

Assisted Solution

by:ppittle
ppittle earned 250 total points
ID: 24852603
I believe document.getElementsByID('<%= lblNumberOfUsers.ClientID%>').value will work as well.

If this or mohan's solution don't work for you, try putting the javascript after the declaration of your label.  In order for the ClientID to return the correct ID, the Control has to have already been added to the page.
0
 

Author Comment

by:jvancojr
ID: 24870571
I get a build error on '<%= lblNumberOfUsers.ClientID%>'
Error      1      'System.Web.UI.WebControls.Label' does not contain a definition for 'ClientId' and no extension method 'ClientId' accepting a first argument of type 'System.Web.UI.WebControls.Label' could be found (are you missing a using directive or an assembly reference?)      C:\Projects\ECommerce CStar\Main\Source\EcomCStarApps\ECommerceWebSitev2\packages.aspx      26      
0
 
LVL 8

Expert Comment

by:ppittle
ID: 24871726
you have to capitalize the last 'd'.  The property is ClientID.  The error indicates you spelled the property as ClientId.
0
 

Author Comment

by:jvancojr
ID: 24895372
I went another direction on this page to get around this issue.
Thanks all for your time
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now