Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain state of a label during postback?

Tags:

jquery

c#

asp.net

 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.

like image 962
Madhan Sekar Avatar asked Nov 02 '22 08:11

Madhan Sekar


1 Answers

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;
like image 189
Adil Avatar answered Nov 09 '22 23:11

Adil