Get dynamically changed asp.net label

I have an asp.net label control web control. I am dynamically changing my text using the jQuery.html () function. When I try to set the current label value on the server, it displays the most original since the page was loaded. Is there any way to make jQuery changes for the tag permanent so that I can upload them to the server?

+4
source share
2 answers

You will need to reflect the jQuery changes in another control that will act on the server side.

For example, use a hidden field that was changed runat="server" shadow of your change to this, then it will be stored on the server, that is, read the label change from the hidden field

So

JQuery

 $('#labelID').html('New Text'); $('#hiddenFieldID').val('New Text'); 

Server side

 string labelText = hiddenFieldID.value; 

This is not very, but this is the only way I found.

JQuery does not interact with ViewState , so any changes to jQuery are lost in the postback. This is one of the reasons why I think that people are now moving towards MVC. The postback architecture does not work well with client-side changes.

+1
source

I do not think that you can do this out of the box (although I could be wrong). The problem is that the label is not an HTML form field, so its value is not sent back to the server during postback, so either the ViewState value will be used, or the default value, not the updated value.

Could you hack something together where you also set the label value in a hidden field that will be sent back to the server via postback and use this to populate the label value during postback if it was changed?

0
source

Source: https://habr.com/ru/post/1411264/


All Articles