CiccioMiami CiccioMiami - 2 months ago 6x
ASP.NET (C#) Question

How to use Javascript to get ASP.NEt Web Forms label's value?

I have the following label control:

<asp:Label ForeColor="DarkGreen" runat="server" ID="lblStatus"></asp:Label>

Its value is filled in the Page_Load event.

I attached the following Javascript (placed at the end of the page, not Master page):

function Validate() {
var lblObj = document.getElementById('<%=lblStatus.ClientID%>');
if (lblObj.value == "Replaced" || lblObj.value == 'Trashed' || lblObj.value == "Internal Use") {

alert("Products with" + lblObj.value + "status cannot be reserved");
return false;

The alert(lblObj.value) displays a popup with text "undefined". How can I fix this problem? Please, I tried many combinations for placing the JavaScript but no luck! Thanks


Browser soruce code:

<span id="ctl00__main_lblStatus" style="color:DarkGreen;">Available</span></td>

First line of Validate JS function:

function Validate() {
var lblObj = document.getElementById('ctl00__main_lblStatus');


Use JQuery and it will run in all browsers and platforms, something like:

$('#<%= lblStatus.ClientID %>').next().text();

source: JQuery: getting the value/text/innerHtml of a checkbox in an ASP.NET CheckBoxList control