On my next postback, tr_1.Visible is true.
Actual server controls like Panel/Label/TextBox/etc when you set the Visible status, checking immediately after gives the status I just set.
Is there some sort of commit function I need to run to make sure ASP.NET gives me the proper status on generic html elements that are runat="server"?
Note, this is a "what do I do to make this work as expected?" not a "what alternatives do I have to doing this" question.
I figured it out. The content was in a panel that wasn't set visible until after the code that set the HtmlElement's visibility. Here's example code replicating what I was experiencing allowing to toggle back and forth between setting the parent panel visibility before or after setting the HtmlElement visibility.
I created a sample page, but for some unknown reason, these forums won't allow me to save the post if I include the aspx lines for the buttons. I've been trying for 40 minutes now.
Here's everything, but you'll have to put your own buttons on the aspx one called b_Visible Text="Visible", the other called b_Hidden with Text="Hide" with the appropriate OnClick events below the CheckBox.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html>
<form id="form1" runat="server">
<asp:Panel runat="server" ID="p_ParentPanel" Visible="false">
<table>
<tr runat="server" id="tr_1">
<td>I'm Visible!</td>
</table>
<asp:Label runat="server" ID="l_Test" Text="Label Visible!" />
</asp:Panel>
HTMLElement Current State:
<asp:Label runat="server" ID="l_tr_ReportedState" Text="" />
should be
<asp:Label runat="server" ID="l_tr_ShouldBe" Text="" />
Label Current State:
<asp:Label runat="server" ID="l_l_ReportedState" Text="" />
should be
<asp:Label runat="server" ID="l_l_ShouldBe" Text="" />
<asp:CheckBox ID="cb_PreSetPanel" runat="server" Text="Set panel visibility first" /><br />
<!-- Place buttons here due to incredibly broken website -->
</form>
</body>
</html>
using System;
public partial class Test : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void b_Visible_Click(object sender, EventArgs e)
HideStuff(true);
protected void b_Hidden_Click(object sender, EventArgs e)
HideStuff(false);
private void HideStuff(bool visible)
if(cb_PreSetPanel.Checked)
p_ParentPanel.Visible = visible; // If I make the parent panel visible first, it gives expected results
tr_1.Visible = visible;
l_Test.Visible = visible;
l_tr_ShouldBe.Text = visible.ToString();
l_tr_ReportedState.Text = tr_1.Visible.ToString();
l_l_ShouldBe.Text = visible.ToString();
l_l_ReportedState.Text = l_Test.Visible.ToString();
if(!cb_PreSetPanel.Checked)
p_ParentPanel.Visible = visible; // Order of operations of parent panel is apparently the culprit
b_Visible.Enabled = !visible;
b_Hidden.Enabled = visible;
I can't reproduce this issue. I assume there is something else going on with the code that we cannot see.
<tr runat="server" id="tr_1">
<td>Test</td>
Code behind
protected void Button1_Click(object sender, EventArgs e)
tr_1.Visible = false;
Label1.Text = tr_1.Visible.ToString();
II want to reply with sample code, but I'm getting:
Access Denied
You don't have permission to access "http://learn.microsoft.com/answers/answers/565322/post.html" on this server.
Reference #18.c90ec617.1632493803.267623b
if(cb_PreSetPanel.Checked)
p_ParentPanel.Visible = visible; // If I make the parent panel visible first, it gives expected results
tr_1.Visible = visible;
l_Test.Visible = visible;
l_tr_ShouldBe.Text = visible.ToString();
l_tr_ReportedState.Text = tr_1.Visible.ToString();
l_l_ShouldBe.Text = visible.ToString();
l_l_ReportedState.Text = l_Test.Visible.ToString();
if(!cb_PreSetPanel.Checked)
p_ParentPanel.Visible = visible; // Order of operations of parent panel is apparently the culprit
b_Visible.Enabled = !visible;
b_Hidden.Enabled = visible;
<asp:Panel runat="server" ID="p_ParentPanel" Visible="false">
<table>
<tr runat="server" id="tr_1">
<td>I'm Visible!</td>
</table>
<asp:Label runat="server" ID="l_Test" Text="Label Visible!" />
</asp:Panel>
HTMLElement Current State:
<asp:Label runat="server" ID="l_tr_ReportedState" Text="" />
should be
<asp:Label runat="server" ID="l_tr_ShouldBe" Text="" />
Label Current State:
<asp:Label runat="server" ID="l_l_ReportedState" Text="" />
should be
<asp:Label runat="server" ID="l_l_ShouldBe" Text="" />