最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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');

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');
Share Improve this question edited Sep 15, 2011 at 13:35 CiccioMiami asked Sep 15, 2011 at 13:13 CiccioMiamiCiccioMiami 8,25634 gold badges96 silver badges156 bronze badges 6
  • you want to set or to get? question's title and content mismatch. – Davide Piras Commented Sep 15, 2011 at 13:17
  • @Davide Piras, I meant get sorry – CiccioMiami Commented Sep 15, 2011 at 13:19
  • @CiccioMiami : can you port the result of validate function's first line by view source ? – Canavar 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 – CiccioMiami 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/… – Davide Piras Commented Sep 15, 2011 at 13:40
 |  Show 1 more comment

6 Answers 6

Reset to default 5

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

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

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

alert(lblObj.innerText);

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

try this

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

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()
发布评论

评论列表(0)

  1. 暂无评论