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.
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 ofddlVesselas the control is loaded.