简体   繁体   English

在服务器端访问Label的值

[英]Accessing value of Label on server side

I have a label on client side. 我在客户端有一个标签。 Its value is updated by javascript. 其值由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. 当我使用:lbltotIntk.text我没有得到任何更新的值。 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..? 还有其他方法可以访问标签值。

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. 隐藏字段是一个很好的解决方案,但是如果网页中有30个标签,则30个隐藏字段是开销。 another alternate is to use css on text box 另一个替代方法是在文本框中使用CSS

   .textBox
   {
   background-color:Transparent;
   border: none;
   }

and set the property ReadOnly of text box to true. 并将文本框的属性ReadOnly设置为true。 now the textbox seems like label.. 现在文本框看起来像标签。

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. 因为在服务器端,该值是从ViewState获取的,但是当我们在客户端中更改某些内容时, ViewState不会相应地更改。 So we get the old value. 因此,我们获得了旧的价值。 That is why hiddenfield is used. 这就是为什么使用hiddenfield的原因。 This problem is not only with Labels, you will have this problem with other server controls. 此问题不仅与标签有关,其他服务器控件也有此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM