4

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%>');
        alert(lblObj.value);
        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

UPDATE

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');
6
  • you want to set or to get? question's title and content mismatch. Commented Sep 15, 2011 at 13:17
  • @Davide Piras, I meant get sorry Commented Sep 15, 2011 at 13:19
  • @CiccioMiami : can you port the result of validate function's first line by view source ? Commented Sep 15, 2011 at 13:28
  • @Davide Piras, see update. However if you want to laugh I just tried the script in IE and it works. It does not work on Firefox tough, I don't mind about IE Commented Sep 15, 2011 at 13:37
  • @CiccioMiami, use JQuery and will run in all browsers and platforms, something like: $("#" + Control.ClientId).next().text(); see here: stackoverflow.com/questions/1286317/… Commented Sep 15, 2011 at 13:40

6 Answers 6

5

labels don't have a value. They have innerHTML and innerText.

Sign up to request clarification or add additional context in comments.

4 Comments

@ Daniel A. White: I tried innerText and still gives me undefined
Is the client id being printed ok?
yes I already compared control and javascript code and they are the same
However if you want to laugh I just tried the script in IE and it works. It does not work on Firefox tough, I don't mind about IE
4

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

Comments

4

Label server control renders as span. So you should get it's content by innerText. try this :

alert(lblObj.innerText);

1 Comment

However if you want to laugh I just tried the script in IE and it works. It does not work on Firefox tough, I don't mind about IE
2

ASP.NET label server control will be rendered in complex HTML output. Like:

<span id="ctl00_ctl00_ContentPlaceHolder1_BodyPlaceHolder_lblLanguage0">
 <label class="inputText">English</label>
</span>

When you use getElementById you will get span. But to set value via javascript you have to access inner label object

Comments

1

try this

<script language="javascript" type="text/javascript">
function getlabelvalue()
{
   var value1 = document.getElementById('<%=labelID.ClientID%>').value;
            if (value1.length < 1)
                value1 = 0;
}    
 </script>

Comments

1

With jquery you need to use the html method.

var g = $('#<%=lblStatus.ClientID%>').html();

These will NOT work with jquery:

  • $('#<%=lblStatus.ClientID%>').innerText
  • $('#<%=lblStatus.ClientID%>').innerHTML
  • $('#<%=lblStatus.ClientID%>').val()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.