当面板设置为Display:None时如何将ASP面板内部的控件设置为Display:none

我在控件内部设置了多个面板,以根据版本号显示/隐藏内容。我有通过页面面板浏览的代码,如果版本号不匹配,则面板设置为 Display:None 。我知道面板设置为 Display:none ,因为我在调试时已在Chrome Developer工具中对其进行了验证。问题在于,包装在面板内的控件仍显示在页面上。以下是在html中设置面板的方式以及将面板设置为显示的代码:无。请指教。谢谢。

如果将面板的visible设置为false,它将起作用,但是即使控件未显示,我仍然需要控件在页面上呈现。

<asp:Panel runat="server" version="1" ID="property113_v1">
    <tr>
        <td class="asterisk">&nbsp;</td>
        <td colspan="2" class="required">How to blank? </td>
    </tr>
    <tr>
        <td>&nbsp;</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">&nbsp;</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"); 
            }
        }
    }

}
firefox_2009 回答:当面板设置为Display:None时如何将ASP面板内部的控件设置为Display:none

您的代码导致无效的html,tr不能是tbody的父代。这引起了您的问题,有关演示,请参阅:https://jsfiddle.net/4n6qkfzw/

您需要使用正确的元素<!-- use the tbody tag with runat=server here --> <tbody runat="server" version="1" ID="property113_v1"> <tr> <td class="asterisk">&nbsp;</td> <td colspan="2" class="required">How to blank? </td> </tr> <tr> <td>&nbsp;</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">&nbsp;</span></asp:RequiredFieldValidator></td> </tr></tbody>

更新您的aspx也使用类似的

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"); 
        }
       */
    }
}

现在将您的代码更新为类似于以下内容的

{{1}}
本文链接:https://www.f2er.com/3155820.html

大家都在问