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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With