Could someone please advise what the "correct" method is for adding HTML content to an ASP.NET page dynamically?
谁能告诉我在ASP中添加HTML内容的“正确”方法是什么?动态网络页面吗?
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.
编辑:我正在构建一个动态的Galleriffic photo图片库,这取决于位于特定文件夹中的图像。
3 个解决方案
#1
26
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
对于控件/文本,我通常使用文字控件并将文本属性设置为我想要添加的HTML,然后可以将该控件添加到您希望它出现的页面的任何位置
LiteralControl reference is here
LiteralControl引用在这里
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);
#2
7
Aspx :
Aspx:
<div id="DIV1" runat="server"></div>
Code behind :
背后的代码:
DIV1.InnerHtml = "some text";
#3
6
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.
- Web用户控件:可以动态添加,并获得Visual Studio的完整编辑器支持。
- XML literals (VB.NET only): Very convenient way to quickly put together HTML in code.
- XML文本(VB。非常方便的方式将HTML快速地放在代码中。
- 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.
- 模板:在您的解决方案中添加一个普通的HTML文档,并将其作为资源包含。然后您将得到编辑器支持,并且您不会用HTML源代码干扰您的代码。
#1
26
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
对于控件/文本,我通常使用文字控件并将文本属性设置为我想要添加的HTML,然后可以将该控件添加到您希望它出现的页面的任何位置
LiteralControl reference is here
LiteralControl引用在这里
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);
#2
7
Aspx :
Aspx:
<div id="DIV1" runat="server"></div>
Code behind :
背后的代码:
DIV1.InnerHtml = "some text";
#3
6
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.
- Web用户控件:可以动态添加,并获得Visual Studio的完整编辑器支持。
- XML literals (VB.NET only): Very convenient way to quickly put together HTML in code.
- XML文本(VB。非常方便的方式将HTML快速地放在代码中。
- 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.
- 模板:在您的解决方案中添加一个普通的HTML文档,并将其作为资源包含。然后您将得到编辑器支持,并且您不会用HTML源代码干扰您的代码。