0
你好,我有一個名爲tableClass的類,它存儲了一個名爲totalHours的雙變量。然後我想將這個變量放在只讀屬性中。問題是totalHours保持重置爲0的值。不知道如果我聲明變量錯誤或什麼不是,我也嘗試將值存儲在viewstate中,並出現相同的問題。有什麼建議麼?任何問題都讓我知道。謝謝!類變量不保持價值
表類
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" />
嗯,你有一點點在這裏。與調試器一起完成。 「hrsTextBox.Text」包含什麼?你確認它被設置爲0以外的值嗎? – 2013-02-21 00:09:38
我在這段代碼中看不到任何對ViewState的使用 – Blachshma 2013-02-21 00:10:00
另外,你確定你正在使用同一個類實例嗎?它不是靜態的類或屬性。 – 2013-02-21 00:10:58