0

我正在尝试使用自定义控件重新创建 ASP.NET 向导控件的功能。我不能使用向导控件,因为它会重新加载页面并将 HTML 包装在table.

控制标准: - 异步重新加载 - 能够从控制 ID 更改步骤(.Next()、.Prev())

它目前由两部分组成:

  • 多重步骤

标记:

<asp:UpdatePanel id="upTest" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="lblbActiveStep" runat="server" Text="Label"></asp:Label>

        <customControl:MultiStep ID="msExample" Visible="True" ActiveStepIndex="0" runat="server">
            <customControl:Step runat="server" Name="Step 1">
                <p>Step 1</p>
            </customControl:Step>

            <customControl:Step runat="server" Name="Step 2">
                <p>Step 2</p>
            </customControl:Step>

            <customControl:Step runat="server" Name="Step 3">
                <p>Step 3</p>
            </customControl:Step>
        </customControl:MultiStep> 

        <asp:Button ID="btnTest" OnClick="btnTest_OnClick" runat="server" Text="Button" />
    </ContentTemplate>
</asp:UpdatePanel>

我当前的代码用于显示活动步骤,但在更新 ActiveStepIndex 时它不会重新渲染。

多步控制:

[ParseChildren(true, "Steps"), PersistChildren(false)]
public class MultiStep : CompositeControl
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public StepList Steps { get; set; }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public int ActiveStepIndex { get; set; }

    public MultiStep()
    { 
        Steps = new StepList();
        ActiveStepIndex = 0;
    }

    protected override void RenderChildren(HtmlTextWriter writer)
    {
        for (int i = 0; i < Steps.Count; i++)
        {
            Steps[i].Visible = (i == ActiveStepIndex);

            if (Steps[i].Visible)
                Steps[i].InstantiateIn(this);
        }

        base.RenderChildren(writer);
    }

    public void Next()
    {
        if (ActiveStepIndex < Steps.Count )
        {
            ActiveStepIndex++;
            UpdateVisible(ActiveStepIndex);
        }
    }

    private void UpdateVisible(int stepIndex)
    {
        foreach (Step step in Steps)
        {
            step.Visible = false;
        }

        Steps[ActiveStepIndex].Visible = true;
    }
}

步进子控制:

[ParseChildren(true, "Content"), PersistChildren(false)]
public class Step : CompositeControl
{

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public string Name { get; set; }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    [TemplateContainer(typeof(MultiStep))]
    [TemplateInstance(TemplateInstance.Single)]
    public ITemplate Content { get; set; }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public Panel Panel { get; set; }

    public Step()
    {
    }

    public void InstantiateIn(Control container)
    {
        Content?.InstantiateIn(container);
    }
}

步骤列表集合

public class StepList : Collection<Step> { }

单击testing按钮时,它会运行以下代码:

protected void testing_OnClick(object sender, EventArgs e)
{
    msExample.Next();
}

没有出现下一步: 多步不工作

更新

该步骤仅更新一次,然后按钮由于某种奇怪的原因不再起作用。 按钮更新控制一次

4

0 回答 0