0

I have created a JavaScript function to get the type of html control and set it to specified value.

function SetControlValue(ctrl, value) {

if (value == undefined)
    return "";

if (document.getElementById(ctrl).type == "text") {
    document.getElementById(ctrl).value = value;
}


else if (document.getElementById(ctrl).type == "label") {

   //document.getElementById(ctrl).innerText = value;
   document.getElementById(ctrl).innerHTML = value;      

} 

return false;

}

On my ASPX page i have created a label as below

<asp:Label id="lblMessage" class="labels"Font-Size="Medium" runat="server"></asp:Label>

and now calling the function

var don="sample text";

SetControlValue('lblMessage', don)

My question is why SetControlValue() function working on text fields but does not work on labels. Is there something that am missing? Thanks.

2 Answers 2

3

The reason why it's not working is because ASP changes the ID of your label. You'll need to get the client side id of the label control.

SetControlValue('<%= lblMessage.ClientID %>', value);

As @Tim B James suggested in the comments you can also set the ClientIDMode to Static as follows:

<asp:Label ClientIDMode="static" id="lblMessage" class="labels" Font-Size="Medium" runat="server"></asp:Label>

Edit: Basically you don't need to check for label or span. Instead you can check if your div element has a property called innerHTML or value. I suggest you to change the javascript function to the following and it should work:

function SetControlValue(ctrl, value) {

    if (value == undefined) {
        return "no value set";
    }

    var element = document.getElementById(ctrl);

    if(!element) {
        return "element not found";
    }

    if(element['value'] !== undefined) {
        element['value'] = value;   
    } else if (element['innerHTML'] !== undefined) {
        element['innerHTML'] = value;   
    }

    return false;
}

Fiddle

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

3 Comments

Also, to add to this, you can set the ClientIDMode of your label to Static this will stop asp.net from changing the id.
Well i tried this it didn't work i was getting this error JavaScript runtime error: Unable to get property 'type' of undefined or null reference
plz check my updated answer! try using nodeName instead!
0

Try this:

document.getElementById(ctrl).innerHTML = value;

Update:

When a asp.net label control is rendered it is Rendered as <span> and not aslabel.

    function SetControlValue(msg) {
    document.getElementById('<%= Label1.ClientID %>').innerHTML = msg;
    var control = document.getElementById('<%= Label1.ClientID %>');
    if (control != null && control.nodeName == "span") {
      alert("its label control");
    }

This works for me!

NOTE: Please make changes in the function according to your needs!

4 Comments

hi @Angwenyi! plz check the edited answer! and let me know..! it works i have tested it!
A moment i try it out.
I get JavaScript runtime error: Unable to get property 'type' of undefined or null referenceon ` var control = document.getElementById('<%= lblMessage.ClientID %>')` I dont know why
that is what i have mentioned just use the 'nodeName' instead of 'type' it will work!

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.