0

您好,我有一个名为 tableClass 的类,它存储一个名为 totalHours 的双变量。然后我想将此变量放在只读属性中。问题是 totalHours 的值不断重置为 0 。不确定我是否声明了变量错误,我也尝试将值存储在视图状态中,并且发生了同样的问题。有什么建议么?任何问题让我知道。谢谢!

表类

Public Class tableClass
    Inherits System.Web.UI.Page
    Private TotalHours As Double = 0

    Private Sub AddNewRow(ByVal hrsTextBox as TextBox)

        TotalHours = TotalHours + CDbl(hrsTextBox.Text)
    End Sub

    ReadOnly Property RegHours() As Double
        'Returns the total hours so far recorded
        Get
            Return TotalHours
        End Get
        End Property
   End Class

默认

Protected Sub btnAddDate_Click(sender As Object, e As EventArgs)
        dim dateTable as New tableClass

        dateTable.AddNewRow(hrsTextbox)
End Sub

 Protected Sub HoursChange(sender As Object, e As EventArgs)
            LabelHoursTotal.Text = (dateTable.RegHours + CDbl(TextBoxHours.Text)).ToString
 End Sub

aspx

<asp:TextBox ID="txtHours" runat="server" width ="90" Text = "0" AutoPostBack="True" OnTextChanged ="HoursChange"></asp:TextBox>

<asp:Label id="lblHoursTotal" runat="server" Text = "0"></asp:Label>

<asp:Button ID="btnAddHour" runat="server" Text="Add New Row" OnClick="btnAddHour_Click" />
4

1 回答 1

1

您需要以某种方式存储该值,当涉及到 Web 应用程序时,根据情况和您的需要,使用 ViewState 或 Session 是一个好主意。ViewState 特定于页面,Session 特定于用户。

于 2013-02-21T00:37:58.760 回答