How to maintain the value of label after postback in Asp.net? [closed]

The label is converted into a span element and the html elements, like span or div, do not have ViewState. Neither the text or html of these is sent
server side like form elements.

Form elements that are posted are the input elements as well as hidden fields. ASP.net maintains ViewState using hidden field and input elements.

I am afraid you have to use hidden fields to maintain the value of labels between postbacks.

HTML

<input id="hdnLabelState" type="hidden" runat="server" >

Javascript

document.getElementById('<%= hdnLabelState.ClientID %>').value = "changed value of span";

Server side (code behind)

string changedLabelValue = hdnLabelState.Value;

Leave a Comment