Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 583
Microsoft Developer Avatar asked Jan 19 '23 05:01

Microsoft Developer


2 Answers

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.

like image 156
David Hedlund Avatar answered Jan 20 '23 18:01

David Hedlund


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.

like image 38
ipr101 Avatar answered Jan 20 '23 18:01

ipr101