Solved

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

Posted on 2009-07-14
5
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

695 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