question

JasonBaginski-0573 avatar image
0 Votes"
JasonBaginski-0573 asked JasonBaginski-0573 answered

Issue with html element runat="server" visibility status

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.

dotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JasonBaginski-0573 avatar image
0 Votes"
JasonBaginski-0573 answered

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;
 }

}


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered JasonBaginski-0573 commented

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();
     }


· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

II want to reply with sample code, but I'm getting:

Access Denied
You don't have permission to access "http://docs.microsoft.com/answers/answers/565322/post.html" on this server.
Reference #18.c90ec617.1632493803.267623b

0 Votes 0 ·

This forum is absolutely horrific. I've tried several times to paste in bits from my full answer reply and when I click Submit, it changes colors and disables and just sits there. Typing this to see if it allows small replies.

0 Votes 0 ·

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 Votes 0 ·

I can post my aspx.cs content, but not my aspx content. Site is so broken.

0 Votes 0 ·
 <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>
0 Votes 0 ·
 <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>
0 Votes 0 ·
         <hr />
         <asp:CheckBox ID="cb_PreSetPanel" runat="server" Text="Set panel visibility first" /><br />
0 Votes 0 ·

I have two buttons below that, one called b_Visible Text="Visible", the other called b_Hidden with Text="Hide" with the appropriate OnClick events. This horrific forum will not save a reply if they are included.

0 Votes 0 ·