Reputation: 194
How to maintain a value assigned to a label through javascript after postback?
Problem: I have assigned a label value using a clientside function. But whenever postback happened, label values are gone.
Solution i found: After searching a lot, all are suggessting to store and retrieve the value to & from a hidden field.
Note: But i want to achieve this without using hidden field as it may
increase pageload time.
Upvotes: 5
Views: 1031
Reputation: 148150
The state of label
is not maintained in ViewState
by asp.net. The labels are converted in to spans and the html of span is not posted on submitting form, this is why changes made by client are not persisted. You can put the state of label in some hidden field when you change it in javascript and access it on server.
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;
Upvotes: 2
Reputation: 749
try an html label, like
<label id="lbl" runat="server">Myv alue</label>
Upvotes: 0
Reputation: 15807
use html hidden field
<input type="hidden" runat="server" id="hiddenlabel">
Upvotes: 0