Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I have a java script function which help to calculate the total cost by taking the price * by the quality
HTML
<script type="text/javascript">

                   $("[id*=txtQuality]").live("change", function () {
                       if (isNaN(parseInt($(this).val()))) {
                           $(this).val('0');
                       } else {
                           $(this).val(parseInt($(this).val()).toString());
                       }
                   });
                   $("[id*=txtQuality]").live("keyup", function () {
                       if (!jQuery.trim($(this).val()) == '') {
                           if (!isNaN(parseFloat($(this).val()))) {
                               var row = $(this).closest("table");
                               $("[id*=lblTotal]", row).html(parseFloat($("[id*=price]", row).html()) * parseFloat($(this).val()));
                           }
                       } else {
                           $(this).val('');
                       }

                   });

    </script>
however when load the default.aspx, the txtQuality will retrieve a count value from the database table , but how the javascript will work if only the table value is change, **but beside that i also want to have a result that when default.aspx is load, the "lblTotal" have a amount calculate by the javascript by using the count value * price**

VB
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then


                Dim txt As TextBox = DirectCast(e.Row.FindControl("txtQuality"), TextBox)
                Dim adapter As New SqlDataAdapter
                Dim ds As New DataSet
                'Dim sql As String

                Dim connectionString = ConfigurationManager.ConnectionStrings("ProjData").ConnectionString
                Dim myConn As New SqlConnection(connectionString)

                Dim cmd = "Select * From Product Where customerID='" & Session("customerID") & "' "

                ' Dim myCmd As New SqlCommand(cmd, myConn)

                Try
                    myConn.Open()
                    Dim myCmd As New SqlCommand(cmd, myConn)
                    adapter.SelectCommand = myCmd
                    adapter.Fill(ds, "Product")
                    adapter.Dispose()
                    myCmd.Dispose()
                    txt.Text = ds.Tables(0).Rows.Count



                Catch ex As Exception
                    MsgBox("Can not open connection ! ")
                End Try
            End If
        End Sub

ASP.NET
<ItemTemplate>


     <table style="width: 79%; height: 31px;">
                        <tr>
                            <td class="style1">
                                <asp:Label ID="Label7" runat="server" Text="price    $"></asp:Label>
                            </td>
                            <td >
                                <asp:Label ID="price" runat="server" Text='<%# Bind("costPerTable") %>' ></asp:Label>

                        </td>
                    </tr>
                    <tr>
                        <td class="style1">
                            <asp:Label ID="Label2" runat="server" Text="Quantity"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
                        </td>
                    </tr>

                    <tr>
                        <td class="style1">
                            <asp:Label ID="Label8" runat="server" Text="Total Cost:"></asp:Label>
                        </td>
                        <td>
                            $<asp:Label ID="lblTotal" runat="server" ></asp:Label>
                        </td>
                    </tr>

                </table>
            </ItemTemplate>

        </asp:TemplateField>
Posted
Updated 30-Jan-12 18:10pm
v2

1 solution

if you are using jquery then document.ready function is your friend but if you are using Microsoft ajax then function pageLoad(sender,args) is your friend and if you are using nothing then is your friend.hope you will find it helpful.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900