Sign up ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have created a SharePoint 2010 visual web part. In the page load event of the user control, i am setting the value of an asp.net label.

<asp:Label ID ="lbMessageIndex" runat="server" Visible="false"></asp:Label>

protected void Page_Load(object sender, EventArgs e)
        {    
           lbMessageIndex.Text = "0";                
        }

I also have a anchor tag with a click event getLabelValues() . In this method, when access the label value using jQuery, empty string value is returned.

function getLabelValues() {
        var index = $('#<%=lbMessageIndex.ClientID%>').text();
        alert(index);
    }

Note - .html() and .val() returns undefined.

share|improve this question

2 Answers 2

This is because of the Label property Visibility="false". Instead use it as:

<asp:Label ID ="lbMessageIndex" runat="server" style="display:none;"></asp:Label>

then you can use:

    $('[id$="lbMessageIndex"]').html();
              OR
    $('[id*="lbMessageIndex"]').html();

To get the label value.

share|improve this answer
up vote 0 down vote accepted

The issue was with the visibility. Control was not rendered when visibility was set to false. The fix is hide it using css. display:none

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.