I have a web panel full of buttons. I want the panel to not be visible at first. But while it's invisible, I want to set certain buttons to visible/invisible and enabled/not enabled.

Then, when I make the panel visible, I want my buttons to have the attributes I set above.

But I'm finding that when I attempt to set buttons to visible while the panel is not visible , the buttons' visible property is not actually getting set.

Is there a way around this?

Thanks,
Randy

Dani AI

Generated

Short expert summary and practical fixes (references to other replies: , , )

When a Panel is hidden on the server (Panel.Visible = false) ASP.NET normally does not emit the panel or its child markup to the browser. That difference between a server-side Visible flag and a client CSS hide (display:none) explains most cases where changing a child Button’s properties "while the panel is invisible" seems to have no effect. ’s tiny repro works because the state changes and reveal happen in the same request; in larger pages lifecycle and viewstate interactions often change the result. (learn.microsoft.com)

Common causes and quick checks

  • ViewState disabled on the page or parent control (state changes won’t persist across postbacks).
  • Controls added dynamically but not recreated early enough (they must exist before ViewState is applied).
  • Page_Load or other code resetting visibility on every postback (missing an IsPostBack guard).
  • Changes made in the wrong lifecycle event (e.g., during Render) can be lost.
    Diagnose with breakpoints in Page_Init / Page_Load / PreRender and inspect the generated HTML to see if the buttons are rendered when the panel is “hidden”. (learn.microsoft.com)

Practical options (pick one)

  • Preferred: keep the panel Visible on the server and hide it client-side with CSS so the child controls are rendered and server code can change them across postbacks. Example pattern:
<!-- markup -->
<asp:Panel ID="pnlButtons" runat="server" CssClass="hidden"> ... </asp:Panel>

<!-- css -->
.hidden { display:none; }

<!-- code-behind -->
// reveal: pnlButtons.CssClass = "";
  • Alternative: if the panel must be server-hidden, store the desired child states (ViewState/Session) and apply them at the moment the panel is made Visible (same request), or set children immediately when toggling the panel. (stackoverflow.com)

Notes and cautions
Hiding with CSS exposes controls in the HTML, so do not rely on client-side disabled/hidden as a security measure — enforce permissions server-side. ’s approach (toggle buttons individually) works but is less maintainable than the CSS-or-apply-on-reveal patterns above.

Recommended Answers

All 3 Replies

Hi,
Can you provide some more details as I am not able to recreate the problem.
I tried the following code and it seems to work fine

.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Pannel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnSet_Click(object sender, EventArgs e)
    {
        p1.Visible = false;
        Button1.Visible = true;
        Button2.Visible = false;
        Button3.Visible = true;
        Button4.Enabled = false;
        Button5.Enabled = true;
    }

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


.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pannel.aspx.cs" Inherits="Pannel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:Panel ID="p1" runat="server" BackColor="#FFC0C0" BorderColor="Red" BorderStyle="Solid" BorderWidth="1px" Direction="LeftToRight" Height="227px" Visible="False" Width="820px">
        <asp:Button ID="Button1" runat="server" Text="B1" /><br />
        <asp:Button ID="Button2" runat="server" Text="B2" /><br />
        <asp:Button ID="Button3" runat="server" Text="B3" /><br />
        <asp:Button ID="Button4" runat="server" Text="B4" /><br />
        <asp:Button ID="Button5" runat="server" Text="B5" /></asp:Panel>
        <asp:Panel ID="Panel2" runat="server">
        </asp:Panel>
        <asp:Button ID="BtnSet" runat="server" OnClick="BtnSet_Click" Text="Set options" />
        <asp:Button ID="btnVisible" runat="server" OnClick="btnVisible_Click" Text="Make Panel Visible" /></div>
    </form>
</body>
</html>

P.K.' Thanks for the response. I'm sorry I'm responding so late.

There's a lot of code, but basically the problem is that the buttons are located on a panel. When the panel is not visible, I can't set any buttons as visible. It doesn't complain, but it doesn't change them.

When the panel is visible, I can set the buttons on and off, of course. If I then make the panel invisible, it does not change the buttons' visibility properties.

hi,

why you set panel to visible or not instead set the buttons to visible or invisible.In both ways you can achieve the same functionality.i can help more if you post your code here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.