Issue with html element runat="server" visibility status

Jason Baginski 101 Reputation points
2021-09-24T11:59:19.14+00:00

I was updating an old project with a new feature a customer requested and found a frustrating problem.

In this particular case, there is an html table in my aspx where a table row is set as runat="server" so the visibility state could be controlled in the aspx.cs. Here's a rough concept of it:

<table>
<tr runat="server" id="tr_1"><td></td></tr>
</table>

Here's the issue.

tr_1.Visible=true;
if(!tr_1.Visible)
  DoesntMakeSense(); // should never happen, but does.

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.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,542 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jason Baginski 101 Reputation points
    2021-09-24T15:10:52.633+00:00

    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>
    <html>
    <body>
    <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>
    </tr>
    </table>
    <asp:Label runat="server" ID="l_Test" Text="Label Visible!" />
    </asp:Panel>
    <hr />
    <div>
    HTMLElement Current State:
    <asp:Label runat="server" ID="l_tr_ReportedState" Text="" />
    should be
    <asp:Label runat="server" ID="l_tr_ShouldBe" Text="" />
    </div>
    <hr />
    <div>
    Label Current State:
    <asp:Label runat="server" ID="l_l_ReportedState" Text="" />
    should be
    <asp:Label runat="server" ID="l_l_ShouldBe" Text="" />
    </div>
    <hr />
    <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;
    }
    

    }

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 28,876 Reputation points
    2021-09-24T14:09:09.337+00:00

    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>
    </tr>
    

    Code behind

        protected void Button1_Click(object sender, EventArgs e)
        {
            tr_1.Visible = false;
            Label1.Text = tr_1.Visible.ToString();
        }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.