无法在更新面板中隐藏使用控件中的面板

时间:2013-10-16 11:47:31

标签: c# asp.net user-controls

我正在动态地在aspx页面中加载usercontrol。一切都在更新面板下加载。

usercontrol包含大约100个面板。它们默认设置为可见false(这样它们就不会呈现为HTML)我试图在aspx页面中单击按钮时设置一个面板。这是完美的。但当我试图从用户控件设置另一个面板时,它也显示前一个面板。

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Panel  runat="server" ID="pnlStandard">          
            //dynamic panels from user control loaded here
        </asp:panel>
        <asp:LinkButton ID="LinkButton1" runat="server" 
                        class="link">test1</asp:LinkButton>
        <asp:LinkButton ID="LinkButton2" runat="server" 
                        class="link">test2</asp:LinkButton>
    </ContentTemplate>
</asp:UpdatePanel>

1 个答案:

答案 0 :(得分:0)

以下代码是我假设您的复制品。我已对此进行了测试,并且按预期工作。

<强>标记式

<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel runat="server" ID="pan1" Visible="false"> 
            Panel 1
        </asp:Panel>

        <asp:Panel runat="server" ID="pan2" Visible="false">
            Panel 2
        </asp:Panel>

        <asp:LinkButton runat="server" ID="lnk1" OnClick="lnk1_Click" Text="Panel1" />
        <asp:LinkButton runat="server" ID="lnk2" OnClick="lnk2_Click" Text="Panel2" />
    </ContentTemplate>
</asp:UpdatePanel>

代码

protected void Page_Load(object sender, EventArgs e)
{
    //if (!Page.IsPostBack)  // you do not want to handle this 
    //{                      // otherwise your panels will only hide on first load

        pan1.Visible = false; //this is required to reset the visibility of the panel back
        pan2.Visible = false; //this is required to reset the visibility of the panel back

    //}   
}

protected void lnk1_Click(object sender, EventArgs e)
{
    pan1.Visible = true;
}

protected void lnk2_Click(object sender, EventArgs e)
{
    pan2.Visible = true;
}