我如何从后面的代码中使用 c# 隐藏 html 列表项 <li>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9632546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:16:27  来源:igfitidea点击:

how can i hide html list item <li> using c# from code behind

c#asp.netc#-4.0code-behind

提问by Fernando

I want to hide html list item that is "li" tag using C#. But i can't do this. In earlier i just hide DIV tag using c#. But i can't hide "li" tag. Please help me to do this .If you can please send your detail Explanation...

我想使用 C# 隐藏“li”标签的 html 列表项。但我不能这样做。早些时候我只是使用 c# 隐藏 DIV 标签。但我无法隐藏“li”标签。请帮我做这件事。如果可以,请发送您的详细说明...

This is my partial code :

这是我的部分代码:

  this.hide.style.Add("display", "none");  // Error in hide 

This is my html code :

这是我的 html 代码:

  <li ID="hide" style="display: Block;"><a href="../list.aspx" >list Approval</a></li>

Please help me to solve this issue ....

请帮我解决这个问题......

回答by Cedric Michel

<asp:Panel ID="Panel1" runat="server">
     <div >
        //place here your list
    </div>
 </asp:Panel>

and with c# you can hide a panel

使用 c# 你可以隐藏面板

回答by Lloyd

You can access a Html item as a GenericHtmlControlby adding the runat='Server' attribute to the markup, you can then access the propertiesprogramatically as if it were a "normal" ASP.Net UI control.

您可以通过向标记添加 runat='Server' 属性来将Html 项作为GenericHtmlControl访问,然后您可以以编程方式访问属性,就好像它是“普通”ASP.Net UI 控件一样。

<li ID="hide" style="display: Block;" runat="Server"><a href="../list.aspx" >list Approval</a></li>

HtmlGenericControl listItem = this.hide as HtmlGenericControl;

if (listItem != null)
    this.hide.style.Add("display", "none");  

回答by GDB

Add an idand runat="server"to your list item:

id和添加runat="server"到您的列表项:

<li id="fooItem" runat="server">
    <%-- ... --%>
</li>

Set the visibility property from code behind (C# example):

从后面的代码中设置可见性属性(C# 示例):

if (someBool)
{
    fooItem.Visible = false;
}

You can also use this approach for applying/removing a style:

您还可以使用这种方法来应用/删除样式:

if (someBool)
{
    fooItem.Attributes.Add("class", "foobar");
    // or removing a style 
    foobarItem.Attributes.Remove("class");
}