Solved

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

Posted on 2009-07-14
5
390 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

829 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