如果标签的可见性设置为false,您如何再次显示标签?

时间:2016-01-23 20:12:44

标签: c# asp.net webforms user-controls

在asp.net webforms中,我有一个带有代码隐藏文件的UserControl(如下所示)。

UserControl中有两个<asp:Label>个节点,我试图将其用作表单中的步骤。

问题是,当我进入“Step2”并单击Cancel时,“Step1”不会重新显示,尽管事实上我将其Visible属性设置为true。

我做错了什么?或者有更好的方法吗?

用户控件

<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="MyUserControl.ascx.cs" Inherits="_MyUserControl" %>

<asp:Label runat="server" ID="lblStep1" Visible="true">
    <fieldset>
        <p>Some initial text here</p>
        <asp:Button runat="server" CssClass="btn btn-primary" ID="cmdSubmit" Text="Submit" />
    </fieldset>
</asp:Label>
<asp:Label runat="server" ID="lblStep2" Visible="false">
    <p>Some text here</p>
    <div>
        <asp:Button runat="server" CssClass="btn btn-primary" ID="cmdRequest" Text="Send The Request" />&nbsp;
        <asp:Button runat="server" CssClass="btn" ID="cmdCancel" Text="Cancel" />
    </div>
</asp:Label>

代码隐藏

public partial class _MyUserControl : UserControl
{

    protected void Page_Init(object sender, EventArgs e)
    {
        cmdCancel.Click  += new EventHandler(cmdCancel_Click);
        cmdSubmit.Click  += new EventHandler(cmdSubmit_Click);
    }

    public void cmdSubmit_Click(object sender, EventArgs e)
    {
        lblStep1.Visible = false;
        lblStep2.Visible = true;
    }

    public void cmdCancel_Click(object sender, EventArgs e)
    {
        // return to the previous step
        lblStep1.Visible = true;
        lblStep2.Visible = false;
    }

}

1 个答案:

答案 0 :(得分:2)

你没有对那些labels做任何事情,并且在标签控件中定义按钮是错误的想法。看起来你正在尝试使用label作为容器控件。而是使用<div>元素/标记作为容器并尝试。它应该工作。

<div runat="server" ID="lblStep1" Visible="true">
    <fieldset>
        <p>Some initial text here</p>
        <asp:Button runat="server" CssClass="btn btn-primary" ID="cmdSubmit" Text="Submit" />
    </fieldset>
</div>
<div runat="server" ID="lblStep2" Visible="false">
    <p>Some text here</p>
    <div>
        <asp:Button runat="server" CssClass="btn btn-primary" ID="cmdRequest" Text="Send The Request" />&nbsp;
        <asp:Button runat="server" CssClass="btn" ID="cmdCancel" Text="Cancel" />
    </div>
</div>