动态添加 HTML 到 ASP.NET 页面

发布于 2024-10-30 02:45:33 字数 313 浏览 1 评论 0原文

有人可以建议将 HTML 内容动态添加到 ASP.NET 页面的“正确”方法是什么吗?

我知道以下声明方法。

//Declaration
<%= MyMethodCall() %>


//And in the code behind.
protected String MyMethodCall()
{
    return "Test Value";
}

有更好的或最佳实践方法吗?

编辑:我正在根据特定文件夹中的图像动态构建 Galleriffic 照片库。

Could someone please advise what the "correct" method is for adding HTML content to an ASP.NET page dynamically?

I am aware of the following declarative method.

//Declaration
<%= MyMethodCall() %>


//And in the code behind.
protected String MyMethodCall()
{
    return "Test Value";
}

Is there a better or best practice way?

EDIT: I am building a Galleriffic photo gallery dynamically depending on the images located in a specific folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮凉 2024-11-06 02:45:33

取决于你想做什么。

对于控件/文本,我通常使用 LiteralControl 并将 Text 属性设置为我要添加的 HTML,然后可以将此控件添加到您想要的页面上的任何位置出现

LiteralControl 参考是
这里

可以看到你想要的 Galleriffic ,我想它会像这样伪出现......

 LiteralControl imageGallery = new LiteralControl();
    string divStart = @"<div id='thumbs'><ul class='thumbs noscript'>";
    imageGallery.Text += divStart;
    foreach ([image in images])
    {
      string imageHTML = @"<li><a class='thumb' name='optionalCustomIdentifier' ref='path/to/slide' title='your image title'>
                           <img src='path/to/thumbnail' alt='your image title again for graceful degradation' /></a>
                           <div class='caption'>[caption]<div></li>";

      imageGallery.Text += imageHTML;
    }
    string divEnd = @"</ul></div>";
    imageGallery.Text += divEnd;

    this.[divOnPage].Controls.Add(imageGallery);

Depends what you want to do.

For controls/text I normally use a LiteralControl and set the Text property as the HTML I want to add, then this control can be added anywhere on the page that you want it to appear

LiteralControl reference is
here

ok seeing as you want it for Galleriffic, I guess it would pseudo-appear as such...

 LiteralControl imageGallery = new LiteralControl();
    string divStart = @"<div id='thumbs'><ul class='thumbs noscript'>";
    imageGallery.Text += divStart;
    foreach ([image in images])
    {
      string imageHTML = @"<li><a class='thumb' name='optionalCustomIdentifier' ref='path/to/slide' title='your image title'>
                           <img src='path/to/thumbnail' alt='your image title again for graceful degradation' /></a>
                           <div class='caption'>[caption]<div></li>";

      imageGallery.Text += imageHTML;
    }
    string divEnd = @"</ul></div>";
    imageGallery.Text += divEnd;

    this.[divOnPage].Controls.Add(imageGallery);
静水深流 2024-11-06 02:45:33

Aspx:

<div id="DIV1" runat="server"></div>

背后代码:

DIV1.InnerHtml = "some text";

Aspx :

<div id="DIV1" runat="server"></div>

Code behind :

DIV1.InnerHtml = "some text";
萌︼了一个春 2024-11-06 02:45:33

有多种方法可以做到这一点,使用哪种方法实际上取决于您的场景和偏好。

  • Web 用户控件:可以动态添加,并且您可以获得 Visual Studio 的完整编辑器支持。
  • XML 文字(仅限 VB.NET):在代码中快速组合 HTML 的非常方便的方法。
  • 模板:将纯 HTML 文档添加到您的解决方案中并将其作为资源包含在内。然后您将获得编辑器支持,并且您的代码不会因 HTML 源代码而变得混乱。

There are several ways to do that, which to use really depends on your scenario and preference.

  • Web User Controls: Can be added dynamically and you get the full editor support of Visual Studio.
  • XML literals (VB.NET only): Very convenient way to quickly put together HTML in code.
  • Templates: Add a plain HTML document to your solution and include it as a resource. Then you'll get editor support and you won't clutter your code with HTML source.
爱的那么颓废 2024-11-06 02:45:33

另一种选择

//.aspx
<asp:Literal ID="myText" runat="server"></asp:Literal>


//.aspx.cs
protected Literal myText;
myText.Text = "Hello, World!";

Another option

//.aspx
<asp:Literal ID="myText" runat="server"></asp:Literal>


//.aspx.cs
protected Literal myText;
myText.Text = "Hello, World!";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文