2013-01-01 67 views
0

我可以將值添加到array(0),但是當我然後將值添加到array(1)它將清除值array(0)。我嘗試了所有我能想到的方式來聲明和創建數組。我的代碼如下所示:陣列不保留更新時的值

Dim aryEstimateInfo() As String = New String(7) {} 

Private Sub wzrdEstimateWizard_NextButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wzrdEstimateWizard.NextButtonClick 

    Select Case wzrdEstimateWizard.ActiveStepIndex 
     Case 0 'first estimate wizard step 
      aryEstimateInfo(0) = rad_lstVehicleType.SelectedItem.ToString 

     Case 1 'second estimate wizard step 
      Dim DamageZoneSelected As Boolean = False 
      For Each cntrl As Control In pnlDamageZone.Controls 
       If TypeOf cntrl Is RadioButton Then 
        Dim RadButton As RadioButton = cntrl 
        If RadButton.Checked Then 
         DamageZoneSelected = True 
         DamageZone = RadButton.Text.ToString 
         Exit For 
        Else 
         DamageZoneSelected = False 
        End If 
       End If 
      Next 
      If DamageZoneSelected = True Then 
       lblDamageZoneError.Visible = False 
       aryEstimateInfo(1) = DamageZone 
      Else 
       'if no damage zone is selected a message is displayed 
       wzrdEstimateWizard.ActiveStepIndex = 2 
       wzrdEstimateWizard.ActiveStepIndex = 1 
       lblDamageZoneError.Visible = True 
      End If 

     Case 2 'third estimate wizard step 
      'assigns the number of dents to the estimate array 
      aryEstimateInfo(2) = ddlNumberOfDents.SelectedValue.ToString 
      'sets the average dent size in the estimate arrau 
      aryEstimateInfo(3) = ddlDentSize.SelectedValue.ToString 
      'sets the add-on code and number of oversized dents 
      If ddlOverSized.Enabled = True Then 
       'aryEstimateInfo.SetValue("3", 4) 
       aryEstimateInfo(4) = "3" 
       aryEstimateInfo(7) = ddlOverSized.SelectedValue.ToString 
      Else 
      End If 
     Case 3 'fourth estimate wizard step 
     Case Else 
    End Select 

End Sub 

在聲明中的代碼的新數組someehere您不能重複我在ASP.Net嚮導控制和基礎,Visual Studio 2010中

+0

你嘗試調試?你確定array(0)的值在特定行之前的某個位置沒有被清除? – Blachshma

+0

您可以在嚮導的step1和step2中顯示代碼嗎? – Steve

+0

我使用代碼即時更新原始帖子使用 –

回答

0

的問題是,每個按鈕的點擊被回發頁面,這會導致您的aryEstimateInfo在每次回發中重新創建。

爲了很好地處理這種情況下,提高了網頁的維護,並使其更容易調試這種情況在未來,我建議以下變化:

1)陣列更改爲與屬性的類:所有的屬性都聲明爲字符串

Public Class EstimateInfo 
    Public VehicleType As String = "" 
    Public DamageZone As String = "" 
    Public NumberOfDents As String = "" 
    Public DentSize As String = "" 
    Public AddOnCode As String = "" 
    Public Oversized As String = "" 
End Class 

注意,但數據類型也許應該改爲以更準確地反映基礎內容。

這種做法將有助於調試,因爲您可以在自動實現的屬性更改爲一個getter/setter方法,這樣就可以放置一個斷點,看看那裏的價值是越來越清:

Private m_sVehicleType As String = "" 
Public Property VehicleType As String 
    Get 
     Return m_sVehicleType 
    End Get 
    Set (Value As String 
     ' You could set a breakpoint here to discover where the value is getting cleared. 
     m_sVehicleType = Value 
    End Set 
End Property 

,如果您需要將字符串數組中的值導出到不同的應用程序或數據庫中,例如,可以向類中添加一個方法來生成適當的字符串數組。

2)向頁面添加一個屬性,將當前答案類存儲在頁面的ViewState中,這樣就不必連續重新填充數組。例如:

Private Property EstimateInfo As EstimateInfo 
    Get 
     ' Add a new record to the viewstate if it doesn't already exist 
     If ViewState("EstimateInfo") Is Nothing Then 
      Me.EstimateInfo = New EstimateInfo 
     End If 
     Return ViewState("EstimateInfo") 
    End Get 
    Set (value As EstimateInfo) 
     ViewState("EstimateInfo") = value 
    End Set 
End Property 

一旦你這樣做,你的嚮導代碼變得更容易理解和維護:

Select Case wzrdEstimateWizard.ActiveStepIndex 
    Case 0 'first estimate wizard step 
     Me.EstimateInfo.VehicleType = rad_lstVehicleType.SelectedItem.ToString 
0

使用此它後又回來了。

我建議的基礎上完成嚮導事件數組 可以使用全部步驟,其中曾經步驟你

我猜控件會被罰款

否則,你需要將陣列存儲後在會話或視圖狀態的每次更新,但我不喜歡這兩個

對不起becoz我用手機我不能查看例如

+0

好吧,那麼你的意思是說在最終的嚮導步驟中,我可以訪問第一個嚮導步驟中的控件?如果是的話,我可以弄清楚如何。非常感謝你,我認爲是這樣的情況 –

+0

是的,即使你在最後的 –

+0

中可以訪問控件,我可以完全看到它的工作,imma測試它然後當它工作il標記你的作爲答案。再次感謝男士們的迅速回復,並感謝所有爲了解決這個問題而回答我的問題。我等了很久才發佈這個。謝謝 –