简体   繁体   English

vb.net中的ASP标签无法捕获在JavaScript中修改的标签文本

[英]Asp Label in vb.net not capturing label text modified in javascript

I have the following label in vb.net: 我在vb.net中具有以下标签:

<div style="height: 20px; vertical-align: text-bottom;">
        <div style="float: left; width: 86%; text-align: right">
            <b>Sub-Total</b> (a) through (e) above:  
        </div>
        <div style="float: right; padding-right: 10px">
            <asp:Label runat="server" ID="lblRateableEmployeesSubtotal" CssClass="lblRateableEmployeesSubtotalCls" Text="0"></asp:Label>
        </div>
</div>

And I am dynamically adjusting this value in from a javascript function: 我正在通过javascript函数动态调整此值:

$('.lblRateableEmployeesSubtotalCls').text(numberWithCommas(subtotal));

This function is called whenever certain textboxes are changed. 每当更改某些文本框时,都会调用此函数。 numberWithCommas just formats the text into a number format (x,xxx). numberWithCommas只是将文本格式化为数字格式(x,xxx)。

When I am trying to save these values in my codebehind, for some reason the labels' text is still showing as "0"! 当我尝试将这些值保存在代码隐藏区中时,由于某种原因,标签的文本仍显示为“ 0”! Even though it is clearly updated on the screen. 即使它在屏幕上已明确更新。

If lblRateableEmployeesSubtotal.Text > "" Then .TotalEmployees = CInt(lblRateableEmployeesSubtotal.Text)

Any idea why this could be happening? 知道为什么会这样吗?

ASP doesn't bother to include this information when it performs a postback, specifically because the whole idea of a label isn't to accept input from the client. ASP在执行回发时不会费心将这些信息包括在内,特别是因为标签的整个思想不是接受来自客户端的输入。 When posting back ASP doesn't send the entire DOM; 回发ASP时不会发送整个DOM; it only sends the information for fields specifically designed to accept input from the client. 它仅发送专门用于接受客户端输入的字段的信息。

In this case, the appropriate tool to use here is a Hidden control. 在这种情况下,此处使用的适当工具是“ Hidden控件。 Add an asp:HiddenField control to the page, set that control's value in your JavaScript code (you can set the label too, of course) and then inspect the value of the hidden field on the server side. 在页面上添加一个asp:HiddenField控件,在JavaScript代码中设置控件的值(当然,您也可以设置标签),然后在服务器端检查隐藏字段的值。

Only Input-type controls are posted back to the server. 仅输入类型的控件回发到服务器。 One possible solution in your case is simulate label with read-only flat-styled TextBox control. 您可能遇到的一种解决方案是使用只读的平面样式TextBox控件来模拟标签。

That jQuery function only changes the text on the client, server doesn't know anything about any changes to the page until you send a POST request, but in that request by default only values of the INPUT elements are sent. 该jQuery函数仅更改客户端上的文本,在发送POST请求之前,服务器对页面的任何更改一无所知,但在该请求中,默认情况下仅发送INPUT元素的值。

If you want to alter some things on the server using javascript on the client you're looking for the AJAX technology. 如果要使用客户端上的javascript更改服务器上的某些内容,则需要AJAX技术。 If not, consider obtaining the value on the server from an input control like text box or make some hidden fields which will be updated in the same time as your label. 如果不是,请考虑从输入控件(如文本框)中获取服务器上的值,或创建一些隐藏字段,这些字段将与标签同时更新。

Sorry for my english. 对不起我的英语不好。

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

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