【问题标题】:array not holding value when updated更新时数组不保存值
【发布时间】:2013-01-01 23:17:10
【问题描述】:

我可以向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 中使用它。

【问题讨论】:

  • 您是否尝试过调试?你确定 array(0) 的值没有在该特定行之前的某处被清除吗?
  • 您能在向导的第 1 步和第 2 步中显示代码吗?
  • 我用我使用的代码更新了原始帖子
  • 我唯一使用该数组的其他代码是我试图在标签中显示这些值:Label1.Text = aryEstimateInfo(0) - 在主要声明中我有这个:Dim aryEstimateInfo () 作为字符串 = 新字符串 (7) {}
  • 认为我们已经解决了这个问题,谢谢大家

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


【解决方案1】:

问题是每次按钮单击都会回发页面,这会导致您的 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

【讨论】:

    【解决方案2】:

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

    我建议在完成向导事件时构建数组 您可以在整个步骤中使用控件

    我想会好的

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

    抱歉,我无法查看示例,因为我使用的是移动设备

    【讨论】:

    • 好的,那么您的意思是,在向导的最后一步我可以访问向导第一步的控件?如果是这样,我可以弄清楚如何。非常感谢,我以为是这样的
    • 是的,即使您进入决赛,您也可以逐步访问控件
    • 好的,解决了所有问题,我完全可以看到它在工作,我会测试它,然后当它工作时我将你的标记为答案。再次感谢男人的快速回复,并感谢所有回复试图帮助我解决这个问题的人。我等了这么久才发布这个。谢谢
    猜你喜欢
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2021-09-14
    • 2016-09-04
    • 2014-12-24
    • 1970-01-01
    相关资源
    最近更新 更多