Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net label value changed in JQuery but unchanged at postback

The original value of the ASP.Net label is "xyz".

I have changed the ASP.Net Label value as below:

$("#<%= lblNew.ClientID %>").text("123");

It changed on the web page. But when I click on the button and get the value of the label, it is changed back to the previous value "xyz" instead of "123".

Response.Write(lblNew.Text);

I have tried to set the html of the label instead of the text as below: but it doesn't work either.

$("#<%= lblNew.ClientID %>").html("123");

How can I get the value changed by Jquery? Thanks.

like image 370
TTCG Avatar asked Jun 26 '13 10:06

TTCG


1 Answers

This is because label text value is loaded from view state.Your jquery change the value of label but didn't change view state where it value is being loaded on postback.... But you want the change label text..so you can get it like this.......

string lblvalue=Request[lblNew.UniqueID] as string;

Here is and example to understand how view state work with label...refrence MSDN

<asp:Label runat="server" ID="lblMessage" 
  Font-Name="Verdana" Text="Hello, World!"></asp:Label>
<br />
<asp:Button runat="server" 
  Text="Change Message" ID="btnSubmit"></asp:Button>
<br />
<asp:Button runat="server" Text="Empty Postback"></asp:Button>
And the code-behind class contains the following event handler for the Button's Click event:
private void btnSubmit_Click(object sender, EventArgs e)
{
  lblMessage.Text = "Goodbye, Everyone!";
}

illustrates the sequence of events that transpire, highlighting why the change to the Label's Text property needs to be stored in the view state. enter image description here

like image 177
Amit Singh Avatar answered Nov 15 '22 04:11

Amit Singh