Reputation: 105
I have a label on client side. Its value is updated by javascript. Now I want to access this updated value on server side. However , as the value is calculated on client side I am not getting this updated value on server side. I may get this updated value using hidden field. But is there any different way to access label value other than using hidden field...?
if (isNaN(tot)) {
document.getElementById('lbltotIntk').value = "0";
} else {
document.getElementById('lbltotIntk').innerText = tot.toFixed(2);
document.getElementById('<%=hdnIntTot.ClientID %>').value = tot.toFixed(2);
}
When I use: lbltotIntk.text I dont get any updated value. You can see here that I have used hidden field here. But I dont want to use that. Is there any other way to access the label value..?
Upvotes: 0
Views: 642
Reputation: 988
The label control is a read-only control... you can't overwrite it from client side and maintain its value if a postback is executed.
so the best solution is to add a hidden field and set the value and then access it from server side.
hidden fields are good solutions but if there are 30 labels in a web page in that case 30 hidden fields are overhead. another alternate is to use css on text box
.textBox
{
background-color:Transparent;
border: none;
}
and set the property ReadOnly of text box to true. now the textbox seems like label..
Upvotes: 3
Reputation: 3117
No, if you are changing something in client side you will not get the updated value in server side.
Because in server side the value is fetched from ViewState
but when we change something in clientside the ViewState
is not changed accordingly. So we get the old value. That is why hiddenfield is used. This problem is not only with Labels, you will have this problem with other server controls.
Upvotes: 0