• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

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

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;
        var radioObj = document.getElementsByName(objName);
        if (!radioObj)
            return "";
        var radioLength = radioObj.length;
        if (radioLength == undefined)
            if (radioObj.checked)
            return radioObj.value;
            return "";
        for (var i = 0; i < radioLength; i++) {
            if (radioObj[i].checked) {
                //return radioObj[i].value;
        return "";

Open in new window

  • 2
  • 2
2 Solutions
this will do

var numberOfGuest = document.getElementsById('<%=lblNumberOfUsers.ClientId%>').innerHTML;
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.
jvancojrAuthor Commented:
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      
you have to capitalize the last 'd'.  The property is ClientID.  The error indicates you spelled the property as ClientId.
jvancojrAuthor Commented:
I went another direction on this page to get around this issue.
Thanks all for your time
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now