数组在更新时不保持值

时间:2013-01-01 23:17:10

标签: asp.net arrays vb.net visual-studio-2010

我可以为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

我在ASP.Net向导控件和基本的visual studio 2010中使用它。

2 个答案:

答案 0 :(得分:0)

当您在代码中声明新数组时,您无法在回发后再次重复使用它。

我建议在完成向导事件上构建数组 你可以在任何步骤中使用控件

我想会没事的

否则你需要在会​​话或视图状态的每次更新后存储数组,但我不喜欢这两个

抱歉,我无法查看示例becoz我正在使用移动设备

答案 1 :(得分: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