2013-01-01 69 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标记你的作为答案。再次感谢男士们的迅速回复,并感谢所有为了解决这个问题而回答我的问题。我等了很久才发布这个。谢谢 –