有条件添加到 ASP.NET 向导步骤的控件显示为控件集合的一部分,无论条件如何
我有一个 ASP.NET 向导步骤,其中包括基于上一步中选择的内容的用户控件:
<asp:WizardStep ID ="WizardStep3" runat="server" Title="Step 3">
<%switch (someFlag)
{
case ("value a"):
%>
<wizStep:Step3_ReportA ID="Step3_ReportA" runat="server" />
break;
case ("value b"):
%>
<wizStep:Step3_ReportB ID="Step3_ReportB" runat="server" />
<%
break;
} %>
</asp:WizardStep>
(注意:这只是伪标记,因此不必担心语法)
这种方法似乎工作正常,并避免动态添加控件到页面的问题以及回发带来的所有持续存在的问题。
但是,我还有以下代码用于验证步骤中选择的内容:
protected void wizReportWizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
int stepId = e.CurrentStepIndex;
var wizardStepControls = wizReportWizard.WizardSteps[stepId].Controls.OfType<BaseWizardStep>().ToList();
foreach (var wizardStepControl in wizardStepControls)
{
if (wizardStepControl.IsStepValid())
{
wizardStepControl.DoSomething();
}
else
{
e.Cancel = true;
return;
}
}
}
我发现令人困惑的是 wizardStepControls
包含两个步骤控件 (Step3_ReportA
和 Step3_ReportB
) - 不仅仅是由于 switch
语句而包含的控件。当然,页面渲染时,只输出一个控件。
那么,为什么 switch 语句中的所有控件都会被报告为属于向导步骤控件集合,以及如何找到 switch 语句实际选择的控件?
提前致谢!
I have a ASP.NET wizard step that includes a user control based on what was selected in a previous step:
<asp:WizardStep ID ="WizardStep3" runat="server" Title="Step 3">
<%switch (someFlag)
{
case ("value a"):
%>
<wizStep:Step3_ReportA ID="Step3_ReportA" runat="server" />
break;
case ("value b"):
%>
<wizStep:Step3_ReportB ID="Step3_ReportB" runat="server" />
<%
break;
} %>
</asp:WizardStep>
(Note: This is just pseudo markup so don't worry about the syntax)
This approach seems to work okay and avoids the problems of adding controls dynamically to the page and all the persisting between postback headaches that brings.
However, I also have the following code that is used to validate what has been selected in the step:
protected void wizReportWizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
int stepId = e.CurrentStepIndex;
var wizardStepControls = wizReportWizard.WizardSteps[stepId].Controls.OfType<BaseWizardStep>().ToList();
foreach (var wizardStepControl in wizardStepControls)
{
if (wizardStepControl.IsStepValid())
{
wizardStepControl.DoSomething();
}
else
{
e.Cancel = true;
return;
}
}
}
What I am finding confusing is that wizardStepControls
contains both step controls (Step3_ReportA
and Step3_ReportB
) - not just the control that has been included due to the switch
statement. Of course, when the page is rendered, only one control is output.
So why do all controls in the switch statement get reported as belonging to the wizard step controls collection and how do I find which is the one that the switch statement has actually selected?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
someFlag 似乎是一个布尔值,您正在字符串上进行比较...而且我看不到这个“someFlag”变量的任何声明,它是布尔值吗?或者它是一个字符串?
someFlag seems to be a boolean and you are comparing on string...Moreover I cannot see any declaration of this "someFlag" variable , is it boolean? or is it a string??