动态添加HTML到ASP.NET页面 [英] Dynamically add HTML to ASP.NET page

查看:196
本文介绍了动态添加HTML到ASP.NET页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人请告知是正确的方法是添加HTML内容的ASP.NET页面动态?

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?

编辑:我建立一个Galleriffic照片库动态取决于位于特定文件夹中的图片

推荐答案

取决于你想要做的事。

有关控制/文我通常使用 LiteralControl 并设置文本财产,因为我想要添加HTML ,那么这种控制可以在任何地方,你希望它出现在页面上添加

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参考是
这里

LiteralControl reference is here

OK看到,因为你想为Galleriffic,我想这将伪出现这样的...

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);

这篇关于动态添加HTML到ASP.NET页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆