Microsoft Developer
Microsoft Developer

Reputation: 5459

Text of label set using lable.innerHTML in javascript is lost after post back

I have set textof lablel to dropdownlist.selectedvalue in javascript. But when I try to fetch that text on button click, its not available. How can I set that value in javascript so that I will be available after postback too.

This is the code of my javascript function.

function ddlVessel_OnSelectedIndexChange() {
    var ddl = document.getElementById("<%=ddlVessel.ClientID %>");
    var lable = document.getElementById("<%=lblSegmentNo.ClientID %>");
    if (ddl.selectedIndex > 0) {
        var SelectedVal = ddl.options[ddl.selectedIndex].value;
        lable.innerText = SelectedVal;
        return true;
    }
    else {
        lable.innerHTML = "";
        return true;
    }
}

I also tried lable.value and lable.text but both are not working.

Upvotes: 4

Views: 7033

Answers (3)

David Hedlund
David Hedlund

Reputation: 129792

Lable is not a form element. It will not be posted to the server, so the server will never know what value you assigned it. Nor is it available in the ViewState, since it was not assigned from the server side.

Normally, you would have to submit this value in a hidden field, and reassign it as the control is loaded, but since you already have access to the new value, in your ddlVessel, you should be able to simply assign the label value to the value of ddlVessel as the control is loaded.

Upvotes: 8

ipr101
ipr101

Reputation: 24236

This isn't a very clean solution, but you could populate a hidden field on changing the label's text. Then check for that on postback and change the label text server side.

Upvotes: 1

Kangkan
Kangkan

Reputation: 15571

Changes done to the HTML in the client side is outside the preview of ASP.NET unless the changes are posted. Any change to a lbel will not be posted back and as such the changes are just temporary.

Upvotes: 0

Related Questions