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

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

4

2 回答 2

0

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

我建议在完成向导事件上构建阵列,您可以在任何时候介入的整个步骤中使用控件

我想会好的

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

抱歉,我无法查看示例,因为我使用的是手机

于 2013-01-01T23:42:01.763 回答
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
于 2013-01-02T00:20:13.803 回答