Label text using lable.innerHTML in javascript gets lost after post back

I set textof lablel to dropdownlist.selectedvalue in javascript. But when I try to extract this text when I click the button, it is not available. How can I set this value in javascript so that I can be accessed after the postback.

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 of them do not work.

+4
source share
3 answers

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

Usually you need to send this value to a hidden field and reassign it when loading the control, but since you already have access to the new value, in ddlVessel you just have to assign the label value to the ddlVessel value when loading the control.

+8
source

This is not a very clean solution, but you can fill in a hidden field when changing the label text. Then check this on the back and change the shortcut text server.

+1
source

Changes made to client-side HTML are outside the ASP.NET preview if the changes are not posted. Any changes to lbel will not be sent back and, as such, the changes are temporary.

0
source

Source: https://habr.com/ru/post/1382440/


All Articles