服务器端显示/隐藏HTML而不产生额外的标记。

时间:2022-04-22 16:01:18

Using something like this to show/hide certain markup:

使用类似的东西来显示/隐藏某些标记:

<asp:Panel runat="server" ID="MyPanel">
    <li>...</li>
    <li>...</li>
    ...
</asp:Panel>

It is rendered out as something like:

它是这样的:

<div>
    <li>...</li>
    <li>...</li>
    ...
</div>

Now I want to have the ability to show/hide the MyPanel server-side but do not want the surrounding div to be rendered.

现在,我希望能够显示/隐藏MyPanel服务器端,但不希望渲染周围的div。

I could do something like:

我可以这样做:

<% if (MyCondition) { %>
    <li>...</li>
    <li>...</li>
    ...
<% } %>

but this looks rather cluttered to me.

但在我看来,这很杂乱。

Or I could do something like:

或者我可以这样做:

<li runat="server" ID="C1">...</li>
<li runat="server" ID="C2">...</li>
...

but this would require all "root" controls to have a runat=server attribute which produces a lot of code and looks cluttered, too, to me.

但是这需要所有的“root”控件都有一个runat=server属性,这个属性会产生很多代码,在我看来也很杂乱。

My question:

我的问题:

Is there a way to tell the Panel control to not produce any markup itself? Or is there another control to use for my purpose?

是否有办法告诉面板控件不要自己生成任何标记?还是有其他控件可以用于我的目的?

2 个解决方案

#1


2  

I've ended up by using the Literal control.

最后我使用了文字控件。

With this control, I could write something like:

有了这个控件,我可以这样写:

<ul>
    <li>1</li>
    <li>2</li>
    <asp:Literal runat="server" Visible="false">
        <li>3</li>
        <li>4</li>
    </asp:Literal>
    <li>5</li>
</ul>

which results in this:

导致:

<ul>
    <li>1</li>
    <li>2</li>
    <li>5</li>
</ul>

And I also could write something like this:

我也可以这样写:

<ul>
    <li>1</li>
    <li>2</li>
    <asp:Literal runat="server" Visible="true">
        <li>3</li>
        <li>4</li>
    </asp:Literal>
    <li>5</li>
</ul>

which results in something like this:

结果是这样的:

<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
</ul>

So this is exactly what I wanted: A server-control that allows me to show/hide multiple HTML markup tags without rendering any additional markup.

这正是我想要的:一个服务器控件,它允许我显示/隐藏多个HTML标记,而不呈现任何其他标记。

#2


1  

If you do not want div generated against Panel then you can make the ul runat server and assign this control some id and show hide it with visible attribute in code behind.

如果您不希望针对面板生成div,那么您可以使ul runat服务器为该控件分配一些id,并在代码后面显示使用可见属性隐藏它。

<li id="myUl" runat="server">

</li>

The panel allows to have scroll bar that are actually achieved through div wont be there by using ul directly.

面板允许有滚动条,实际上通过div实现的滚动条不会直接使用ul。

#1


2  

I've ended up by using the Literal control.

最后我使用了文字控件。

With this control, I could write something like:

有了这个控件,我可以这样写:

<ul>
    <li>1</li>
    <li>2</li>
    <asp:Literal runat="server" Visible="false">
        <li>3</li>
        <li>4</li>
    </asp:Literal>
    <li>5</li>
</ul>

which results in this:

导致:

<ul>
    <li>1</li>
    <li>2</li>
    <li>5</li>
</ul>

And I also could write something like this:

我也可以这样写:

<ul>
    <li>1</li>
    <li>2</li>
    <asp:Literal runat="server" Visible="true">
        <li>3</li>
        <li>4</li>
    </asp:Literal>
    <li>5</li>
</ul>

which results in something like this:

结果是这样的:

<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
</ul>

So this is exactly what I wanted: A server-control that allows me to show/hide multiple HTML markup tags without rendering any additional markup.

这正是我想要的:一个服务器控件,它允许我显示/隐藏多个HTML标记,而不呈现任何其他标记。

#2


1  

If you do not want div generated against Panel then you can make the ul runat server and assign this control some id and show hide it with visible attribute in code behind.

如果您不希望针对面板生成div,那么您可以使ul runat服务器为该控件分配一些id,并在代码后面显示使用可见属性隐藏它。

<li id="myUl" runat="server">

</li>

The panel allows to have scroll bar that are actually achieved through div wont be there by using ul directly.

面板允许有滚动条,实际上通过div实现的滚动条不会直接使用ul。