1

I have a label and change it's value with javascript like this:

document.getElementById("lb").InnerHTML = "10";
document.getElementById("lb").InnerText = "10";

In the page, the label value is changed but in code behind when i want use lb. text it shows the old value, not 10.

How can change the value with javascript (not JQuery)(not hidden field) and see the new value in the code behind?

Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117

1 Answers1

3

aspnet label renders as span in to the client side:

I think that only controls that render as input and changed by javascript are updated on viewstate, so you can do this;

<asp:Label runat="server" Text="salam" ID="lb" ></asp:Label>
<asp:HiddenField id="hdlbl" runat="server" />

document.getElementById("lb").InnerHTML = "10"; // update client side
document.getElementById("hdlbl").value = "10";  // store value to code behind

C#

hdlbl.value ...
Sandcar
  • 892
  • 1
  • 9
  • 21
  • thanks for your answer but i don't want use hiddenfield i want to find a way change lable not hidden field. – Mahdi_Nine Dec 15 '13 at 04:09
  • @Mahdi_Nine What you plan to do will be difficult if not impossible due to the way the aspnet works. I had a similar problem and had to use a solution like this. Change control with js and keep tracking changes with a hidden field :( – Sandcar Dec 15 '13 at 17:02