在 ASP.NET 中显示大型多行 HTML 内容

Display large multi-line HTML content in ASP.NET

在经典中 ASP, 我可以像这样轻松显示 HTML 内容:

    <% if a=b then %>
    <div>line 1 (a static content1 here)</div>
    <div>line 2 (a static content2 here)div>
    <div>line 3 (a static content3 here)</div>
    <div>... (more than 10000 lines, no problem)</div>
    <% end if %>

但是我怎样才能在 ASP.NET 中轻松做到这一点?

我可以用 Response.Writeline

写一些行
Response.writeLine("<div>Line 1</div>")
Response.writeLine("<div>Line 2</div>")
Response.writeLine("<div>Line 3</div>")
....

但是显示大内容太难了

VBScript 和 vb.net 是不同的语言,但它们具有相似的语法,因此您上面的非常简单的示例实际上应该适用于 VB.net。你需要记住的一件事是 VB.net 默认情况下 Option Explicit 是打开的,因此你必须声明你的变量。 (最好在 Classic ASP VBScript 中打开它)。以下肯定有效

<% Dim a As String, b As String
        a = "ABCD"
        b = "ABCD"
        If a = b Then
%>
     <p> Your 10000 lines of code here</p>

<% End If %>

然而,虽然没有什么可以阻止您在 .aspx 文件中使用经典 asp 样式的 scriptlet,但 ASP.net 提供了更好的选择。我建议你看看<asp:Literal>控件,例如:

<asp:Literal ID = "EFGH" runat="server" Visible="false">
Your 10000 lines of code
</asp:Literal>

然后在代码隐藏中你可以使用

Dim a As String, b As String
    a = "ABCD"
    b = "ABCD"

    If a = b Then
        EFGH.Visible = true
    End If

以便代码隐藏更改控件Visible 属性

一般来说,从代码隐藏中将 HTML 内容写入您的页面并不是最好的主意。不匹配 HTML 标签等的机会太多

话虽这么说,如果你想做...有几个选择。

标签。这将使您可以通过该文字标记将一些 HTML (或原始内容)添加到页面中。如果您想呈现来自数据库或 Web 服务的 HTML,这将非常方便。

如果您只想 hide/show 东西,那么将 HTML 包裹在 asp:panel 标签中是个好主意。然后你从 Visual Studio 得到所有的 HTML 语法检查,你只需要 show/hide 该面板就可以了。