user3865601
user3865601

Reputation: 39

TextBox value that was set by JQuery is not avaliable in serverside page load event

I have set a textbox value by calling a jQuery function .The textox value is assigned perfectly but it is not on server side .it is showing empty. This is my script side code.

<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>

Function

<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>

on server side

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

((TextBox)row.FindControl("txtAmount")).Text return empty

Upvotes: 0

Views: 1697

Answers (1)

Adil
Adil

Reputation: 148120

The value changed by client side scripting language like javascript is not available in server side as Server Side uses ViewState to get the value of form controls. You can use a hidden field assign the value to it in javascript and get the hidden field on server side.

Html

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

JavaScript

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

Code behind

string hdnValue = hdn.Value;

Upvotes: 2

Related Questions