我有几个从以下抽象类继承的用户控件:
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
但当然,这样做会产生运行时错误,因为编译器无法进行强制转换。
有任何想法吗?