0
votes

i have one textbox in grivew and rest is DataField ad by default in gridview and
Im entering Data in gridview with the help of ViewState Data Enter smoothly and when i enter quantity in Textbox which is in Gridvew its entered but one i click on Save Button It Refresh the textbox to 0 and rest all data remain same only textbox value reset even when im trying to enter second product in gridview its again refresh textbox to 0 even previous i entered any quantity ..i dont know how to resolve it
Here is the code below On Load Page

  Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(3) {New DataColumn("ID"), New DataColumn("Item"), New DataColumn("Price"), New DataColumn("txtQuantity")})
        ViewState("Customers") = dt

    End If
End Sub

On Button Click Event

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt As DataTable = DirectCast(ViewState("Customers"), DataTable)
    dt.Rows.Add(TextBox2.Text.Trim, itemcode.Trim(), 1000)
    ViewState("Customers") = dt
    GridView1.DataSource = dt
    GridView1.DataBind()
End Sub

and in Design View

<Columns>
     <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-Width="10%"/>
    <asp:BoundField DataField="Item" HeaderText="Item" ItemStyle-Width="50%"/>
    <asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-CssClass="price" ItemStyle-Width="10%"/>

    <asp:TemplateField HeaderText="Quantity"  ItemStyle-Width="10%">
        <ItemTemplate>
            <asp:TextBox ID="txtQuantity" AutoPostBack="false"  runat="server" ></asp:TextBox>

        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Total" ItemStyle-Width="20%">
        <ItemTemplate>
            <asp:Label ID="lblTotal" AutoPostBack="false" runat="server" Text="0"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

Except txtQuantity And lblTotal rest of All Value Remain Same But only text box and lblTotal reset to 0

Here is the java script which im using to multiply gridview column

<script type="text/javascript">
$(function () {
    $("[id*=txtQuantity]").val("0");
});
$("body").on("change keyup", "[id*=txtQuantity]", function () {
    //Check whether Quantity value is valid Float number.
    var quantity = parseFloat($.trim($(this).val()));
    if (isNaN(quantity)) {
        quantity = 0;
    }

    //Update the Quantity TextBox.
    $(this).val(quantity);

    //Calculate and update Row Total.
    var row = $(this).closest("tr");
    $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat($(this).val()));

    //Calculate and update Grand Total.
    var grandTotal = 0;
    $("[id*=lblTotal]").each(function () {
        grandTotal = grandTotal + parseFloat($(this).html());
    });
    $("[id*=lblGrandTotal]").html(grandTotal.toString());
});
1

1 Answers

0
votes

In the lblTotal markup you are setting the Text="0", so on postback (button click) it will show