I have set up multiple panels inside controls to show/hide things depending on a version number. I have code that goes through panels on the page and if the version number does not match then the panel is set to Display:None. I know the panels are set to Display:none because I verified it in Chrome Developer tools while debugging. The problem is that the controls wrapped inside the panels are still shown on the page. Below is how the panel is setup in the html and the code that sets the panels to display:none. Please advise. Thank you.
It works if visible is set to false for the panel however I still need the controls to render on the page even if they arent shown.
<asp:Panel runat="server" version="1" ID="property113_v1">
<tr>
<td class="asterisk"> </td>
<td colspan="2" class="required">How to blank? </td>
</tr>
<tr>
<td> </td>
<td colspan="2"><asp:RadioButtonList ID="property113" runat="server" RepeatDirection="horizontal" RepeatLayout="flow">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="property113Validator" runat="server" ControlToValidate="property113"
ErrorMessage="" Display="dynamic" ValidationGroup="Page"><span class="validator"> </span></asp:RequiredFieldValidator></td>
</tr></asp:Panel>
foreach (Panel pnl in p)
{
if (pnl.Attributes["Version"] != null)
{
if (pnl.Attributes["Version"] == ver)
{
pnl.Style.Add(HtmlTextWriterStyle.Display, "");
}
else
{
pnl.Style.Add(HtmlTextWriterStyle.Display, "none");
}
}
}
}
Your code results in invalid html, div
can not be a parent of tr
. This is causing your problem, for a demo see: https://jsfiddle.net/4n6qkfzw/
You need to use the correct element tbody
Update your aspx to use similar too
<!-- use the tbody tag with runat=server here -->
<tbody runat="server" version="1" ID="property113_v1">
<tr>
<td class="asterisk"> </td>
<td colspan="2" class="required">How to blank? </td>
</tr>
<tr>
<td> </td>
<td colspan="2"><asp:RadioButtonList ID="property113" runat="server" RepeatDirection="horizontal" RepeatLayout="flow">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="property113Validator" runat="server" ControlToValidate="property113"
ErrorMessage="" Display="dynamic" ValidationGroup="Page"><span class="validator"> </span></asp:RequiredFieldValidator></td>
</tr></tbody>
Now update your code behind to something similar to:
List<HtmlGenericControl> tbodies = new List<HtmlGenericControl>{/*Populate your list here*/};
foreach (HtmlGenericControl tbody in tbodies)
{
if (tbody.Attributes["version"] != null)
{
if (tbody.Attributes["version"] == ver)
{
tbody.Style.Add(HtmlTextWriterStyle.Display, "");
} /*Probably actually don't need the else statment
else
{
pnl.Style.Add(HtmlTextWriterStyle.Display, "none");
}
*/
}
}