如何用textchanged计算值

时间:2013-12-30 02:12:55

标签: asp.net sql vb.net

我正在尝试使用textchanged函数进行一些简单的计算,但它似乎不起作用。所以这就是我所拥有的。

1。 从sql获得cost1和cost2

cost1.Text = ODBCdataset.Tables("tbl_vehiclemanagement").Rows(0).Item(36)
cost2.Text = ODBCdataset.Tables("tbl_vehiclemanagement").Rows(0).Item(38)
totalcost.Text = ODBCdataset.Tables("tbl_vehiclemanagement").Rows(0).Item(86)

SQL数据类型是小数。

2。 将新值插入cost1和cost2。基本上我试图获得cost1 + cost2 = totalcost然后将其保存到mysql。

  1. 手动将其添加到sql中不是问题。问题是将totalcost自动计算或至少计算我何时更新mysql。
  2. 我制作了3个文本框。

    <asp:TextBox ID="cost1"  OnTextChanged="cost1_TextChanged" AutoPostBack="true"  runat="server"></asp:TextBox>
    <asp:TextBox ID="cost2"  OnTextChanged="cost2_TextChanged" AutoPostBack="true"  runat="server"></asp:TextBox>
    <asp:TextBox ID="totalcost" runat="server"></asp:TextBox>
    
    
    
    Private Sub CalculateAll()
    totalcost.Text = cost1.Text + cost2.Text
    End Sub
    
    Protected Sub cost1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cost1.TextChanged
            Call CalculateAll()
        End Sub
    
    Protected Sub cost2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cost2.TextChanged
            Call CalculateAll()
        End Sub
    

    问题在于文本更改totalcost变为000.000.000.000.00并且我输入cost1的任何内容都成为初始值。请帮忙。谢谢。如果信息不够,请告诉我。感谢

2 个答案:

答案 0 :(得分:3)

您的Sub应如下所示:

Private Sub CalculateAll()
    Dim cost_1 As Decimal = 0D
    Dim cost_2 As Decimal = 0D
    Dim total As Decimal = 0D
    Decimal.TryParse(cost1.Text, cost_1)
    Decimal.TryParse(cost2.Text, cost_2)

    total = cost_1 + cost_2
    totalcost.Text = total.ToString()
End Sub

编辑:

保存到Db时,您必须将字符串转换为十进制。我建议使用参数化查询:

    Dim cost_1 As Decimal = 0D
    Dim cost_2 As Decimal = 0D
    Dim total As Decimal = 0D
    Dim id As String = Request.QueryString("id")
    Decimal.TryParse(cost1.Text, cost_1)
    Decimal.TryParse(cost2.Text, cost_2)
    Decimal.TryParse(totalcost.Text, total)

    Dim cmd As SqlCommand = New SqlCommand()
    Dim sqlquery As String = "UPDATE tbl_vehiclemanagement SET f_usercosting=@usr, f_allinprice=@totalcost, f_expenses1=@cost1, f_expenses2=@cost2 WHERE f_id=@id"
    cmd.CommandText = sqlquery

    cmd.Parameters.AddWithValue("usr", usr)
    cmd.Parameters.AddWithValue("totalcost", totalcost)
    cmd.Parameters.AddWithValue("cost1", cost1)
    cmd.Parameters.AddWithValue("cost2", cost2)
    cmd.Parameters.AddWithValue("id", id)
    'Open connection and execute the query here

编辑2: 如果要恢复文本框的初始值,可以将值保存到viewstate并在重置时检索它们。

在文本框中检索值时,还要将它们保存在viewstate中:

cost1.Text = ODBCdataset.Tables("tbl_vehiclemanagement").Rows(0).Item(36)
cost2.Text = ODBCdataset.Tables("tbl_vehiclemanagement").Rows(0).Item(38)
totalcost.Text = ODBCdataset.Tables("tbl_vehiclemanagement").Rows(0).Item(86)

' Add these lines to save values in ViewState
ViewState("cost1") = cost1.Text
ViewState("cost2") = cost2.Text
ViewState("totalcost") = totalcost.Text

添加重置按钮:

<asp:Button ID="Reset" OnClick="Reset_Click" runat="server"  Text="Button" />

按钮事件恢复文本框值。但首先检查viewstate是否为null:

Protected Sub Reset_Click(sender As Object, e As EventArgs) Handles Reset.Click

    cost1.Text = "0.00"
    cost2.Text =  "0.00"
    totalcost.Text =  "0.00"

    If (ViewState("cost1")) IsNot Nothing Then cost1.Text = ViewState("cost1").ToString()
    If (ViewState("cost2")) IsNot Nothing Then cost2.Text = ViewState("cost2").ToString()
    If (ViewState("totalcost")) IsNot Nothing Then totalcost.Text = ViewState("totalcost").ToString()
End Sub

答案 1 :(得分:0)

试试这个:

Protected Sub cost1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cost1.TextChanged

        If cost2.text = Nothing Then
         'add code here if you want
        Else    
         Call CalculateAll()
        End if

End Sub

Protected Sub cost2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cost2.TextChanged

         If cost1.text = Nothing Then
            'add code here if you want
         Else
            Call CalculateAll()
         End If

End Sub