Greetings. It turns out that I have one Textbox
that changes its background color when it loses focus, all this thanks to a function that I have over there in JavaScript
. Later I have another one Textbox
that does PostBack
what I need for a query to a database, and here is the problem:
when that second TextBox
does PostBack
, the background color of the first one loses its property that it had acquired thanks to the function of JavaScript
. And it makes sense, I know.
But is there any way to make PostBack not affect the characteristics of the controls that are acquired from the client side? Or maybe doing something in it Page_Load
? And if the answer is in the latter, I don't know exactly what.
To make myself understand a little better, I have tried this in a script
:
document.getElementById('<%=Label1.ClientID%>').innerHTML= "Nuevo_texto";
The above changes the text of a Label
, doesn't it?
So far, normal, but if I then do a PostBack
, it Label
takes its default value.
How do I avoid this? It's possible?
The way I was able to fix it was by saving the state of the control to another control on the server, such as a field
HiddenField
. Whenever a change is made in your control, you add the change as an attribute to the server control. At the time of postback you read the control information and apply it to your control.JS:
So in the
Page_Load
, when doing postback you read the attributes of theHiddenField
and apply it to the control:}
When doing
PostBack
it, webforms does not send the style information of each element to the server because of how heavy each request of so much information would be.