使用无效工作的面板进行自定义控制

时间:2018-04-11 15:20:10

标签: c# asp.net asp.net-webcontrol

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

控制标准: - 异步重新加载 - 能够从控件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时,它不会重新呈现。

MultiStep Control:

[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);
    }
}

StepList Collection

public class StepList : Collection<Step> { }

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

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

未显示下一步: Multi Step not working

更新

该步骤仅更新一次,然后按钮因某些奇怪的原因不再起作用。 Button update control once

0 个答案:

没有答案