0

我有几个从以下抽象类继承的用户控件:

Public MustInherit Class CustomVRAUserControl
    Inherits System.Web.UI.UserControl

    Public MustOverride Sub ClearErrors()
    Public MustOverride Sub UpdateLabels()
    Public MustOverride Function Validate() As Boolean
    Public MustOverride Sub UpdateSession()

End Class

我有一个使用用户控件的向导。对于某些条件,有一些动态的工作删除步骤。这是向导的示例:

<asp:Wizard ID="VRAWizard" runat="server"......>    
        <WizardSteps>
            <asp:WizardStep ID="wizResidentialAddress" runat="server" 
                Title="RESIDENTIAL ADDRESS" StepType="Step">
                  <uc2:ResidentialAddress ID="uctlResidentialAddress" runat="server" />                       
            </asp:WizardStep>

            <asp:WizardStep ID="wizMailingAddress" runat="server" 
                Title="MAILING ADDRESS"  StepType="Step">
                  <uc3:MailingAddress ID="uctlMailingAddress" runat="server" /> 
            </asp:WizardStep>
        </WizardSteps>
    </asp:Wizard>

在我的向导类后面的代码中,我在 NextButton 单击上做了一些工作:

Protected Sub VRAWizard_NextButtonClick(sender As Object, e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles VRAWizard.NextButtonClick

        If VRAWizard.ActiveStep.ID = "wizResidentialAddress" Then
            uctlResidentialAddress.ClearErrors()
            If Not (uctlResidentialAddress.validate) Then
                uctlResidentialAddress.UpdateLabels()
                e.Cancel = True
            Else
                uctlResidentialAddress.UpdateSession()
            End If
        End If


        If VRAWizard.ActiveStep.ID = "wizMailingAddress" Then
            uctlMailingAddress.ClearErrors()
            If Not (uctlMailingAddress.validate) Then
                uctlMailingAddress.updateLabels()
                e.Cancel = True
            Else
                uctlMailingAddress.UpdateSession()
            End If
        End If

显然,这是相当重复的。

我想做类似......

CType(VRAWizard.ActiveStep.Controls.Item(0), CustomVRAUserControl).ClearErrors()

    If Not (CType(VRAWizard.ActiveStep.Controls.Item(0), CustomVRAUserControl).Validate()) Then
        CType(VRAWizard.ActiveStep.Controls.Item(0), CustomVRAUserControl).UpdateLabels()
        e.Cancel = True
    Else
        CType(VRAWizard.ActiveStep.Controls.Item(0), CustomVRAUserControl).UpdateSession()
    End If

但当然,这样做会产生运行时错误,因为编译器无法进行强制转换。

有任何想法吗?

4

1 回答 1

1

它应该能够处理演员表。我认为集合中的第一个控件是文字,所以你必须这样做:

CType(VRAWizard.ActiveStep.Controls.Item(1), CustomVRAUserControl).ClearErrors() 

既然你再次使用它,你不妨将它设置为一个变量:

Dim objVRAControl As CustomVRAUserControl = CType(VRAWizard.ActiveStep.Controls.Item(1), CustomVRAUserControl) 

然后你可以这样做:

objVRAControl.ClearErrors()

如果这不起作用,请使用 BreakPoint 和 Watch 变量来确保您使用的是正确的索引。

于 2012-05-11T21:31:52.267 回答