在向导步骤中的用户控件上调用函数

时间:2012-05-11 17:46:00

标签: asp.net vb.net webforms

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

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

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

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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

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

由于您再次使用它,您也可以将其设置为变量:

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

然后你可以这样做:

objVRAControl.ClearErrors()

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