由JQuery设置的文本框值在服务器端页面加载事件中不可用

本文关键字:事件 加载 服务器端 设置 JQuery 文本 | 更新日期: 2023-09-27 18:14:51

我通过调用jQuery函数设置了一个文本框值。textox值被完美地分配,但它不在服务器端。它显示为空。这是我的脚本代码。

<asp:TemplateField HeaderText="Quantity" AccessibleHeaderText="Quantity" ItemStyle-VerticalAlign="Middle">
                                                                                    <ItemTemplate>
                                                                                        <asp:TextBox ID="txtQuantity" runat="server" MaxLength="5" ValidationGroup="a" BorderStyle="Solid" Text='<%#Eval("QTY") %>'
                                                                                            Width="50px" BorderColor="#CCCCFF" onkeypress="return AllowNumbersOnly(this,event)" AutoPostBack='false' 
                                                                                            OnChange="javascript:totalCalc();" Height="12px"></asp:TextBox>
                                                                                    </ItemTemplate>
                                                                                    <HeaderStyle Width="50px" BackColor="#A8AEBD"/>
                                                                                    <ItemStyle Width="50px" Height="12px" />
                                                                                </asp:TemplateField>
                                                                                <asp:TemplateField HeaderText="Rate">
                                                                                    <ItemTemplate>
                                                                                        <asp:Label ID="lblRate" runat="server" DataField="RATE" Text='<%#Eval("RATE") %>'
                                                                                            ItemStyle-CssClass="price" Height="12px" />
                                                                                    </ItemTemplate>
                                                                                    <FooterTemplate>
                                                                                        <asp:Label ID="lbltxttotal" runat="server" Text="Total: "  Font-Bold="True" />
                                                                                    </FooterTemplate>
                                                                                    <HeaderStyle Width="100px" BackColor="#A8AEBD" />
                                                                                    <ItemStyle Width="100px" Height="12px" />
                                                                                </asp:TemplateField>

函数
<script type="text/javascript">
    $(function () {
        $("[id*=txtQuantity]").val("0");
    });
    $("[id*=txtQuantity]").live("change", function () {
        if (isNaN(parseInt($(this).val()))) {
            $(this).val('0');
        } else {
            $(this).val(parseInt($(this).val()).toString());
        }
    });
    $("[id*=txtQuantity]").live("keyup", function () {
        if (!jQuery.trim($(this).val()) == '') {
            if (!isNaN(parseFloat($(this).val()))) {
                var row = $(this).closest("tr");
                var smt = parseFloat($("[id*=lblRate]", row).html()) * parseFloat($(this).val());
                var hid = (parseFloat($("[id*=lblRate]", row).html()) * parseFloat($(this).val())).toFixed(2);
                $("input[id*='txtAmount']", row).val(hid);
            }
        } else {
            var row = $(this).closest("tr");
            $("input[id*='txtAmount']", row).val("0");
            $(this).val('');
        }
        var total = 0.00;
        $("#gv1 input[id $= 'txtAmount']").each(function () {
            var value = $(this).val();
            total = parseFloat(total) + Math.round(parseFloat(value));
        });
        $("#txtTotalAmount").val(total);
        $("#lblTotal").val(total);
    });
</script>

服务器端

 drow[0]["TOTAL"] = Convert.ToDecimal(((TextBox)row.FindControl("txtAmount")).Text);
((TextBox)row.FindControl("txtAmount")).Text return empty

由JQuery设置的文本框值在服务器端页面加载事件中不可用

由客户端脚本语言(如javascript)更改的值在服务器端不可用,因为服务器端使用ViewState来获取表单控件的值。您可以使用hidden字段在javascript中分配值,并在服务器端获得hidden字段。

<input type="hidden" id="hdn" runat="server" />
JavaScript

document.getElementById("hdn").value = "your value";

代码后面

string hdnValue = hdn.Value;