Craig M
Craig M

Reputation: 5628

Panel.Enabled works inside UpdatePanel. Panel.Visible does not

I have a Panel control inside of an UpdatePanel. When I set Panel.Enabled = false; on postback, all controls inside the Panel become disabled. However, when I call Panel.Visible = false; on postback, the Panel still displays.

This code does as expected:

protected void rdoPayment_CheckedChanged(object sender, EventArgs e)
{
    pnlBillingAddress.Enabled = rdoCreditCard.Checked;
    upBillingAddress.Update();
}

If I change the code to this, the panel is still visible when it's set to false:

protected void rdoPayment_CheckedChanged(object sender, EventArgs e)
{
    pnlBillingAddress.Visible = rdoCreditCard.Checked;
    upBillingAddress.Update();
}

Further, if I change the code like so, when Enabled is set to false, the controls no longer become disabled and the panel is still visible:

protected void rdoPayment_CheckedChanged(object sender, EventArgs e)
{
    pnlBillingAddress.Enabled = rdoCreditCard.Checked;
    pnlBillingAddress.Visible = rdoCreditCard.Checked;
    upBillingAddress.Update();
}

Anyone have any idea what's going on here?

ps. I can post the aspx portion of the code, but it's really long so I'll only post it if it's absolutely needed.

Upvotes: 1

Views: 3283

Answers (2)

Ahmad Mageed
Ahmad Mageed

Reputation: 96557

The Panel's Visible property is returning true since its parent control's visibility is also set to true. This isn't well documented, but here's a relevant blog post: ASP.Net: Remember, the .Visible property also checks parent’s visibility!

Perhaps you'll need to refactor your code and use a separate UpdatePanel for just that Panel and set the UpdatePanel's visibility instead. I suggest setting a breakpoint and checking the visibility after testing with different values for the control and its parent.

While the above link seems accurate for the PlaceHolder control and others it seemed very odd if it applied to everything since this should be a common scenario. I got around to testing this and setting visibility to a Panel within an UpdatePanel works fine.

Upvotes: 2

Craig M
Craig M

Reputation: 5628

Upon closer inspection of the aspx, I realized that the Panel was actually wrapping the UpdatePanel instead of vice versa. I put the Panel inside of the ContentTemplate and all is well now.

Upvotes: 4

Related Questions