Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将HTML动态添加到ASP.NET页面_C#_Asp.net_Html_Declarative - Fatal编程技术网

C# 将HTML动态添加到ASP.NET页面

C# 将HTML动态添加到ASP.NET页面,c#,asp.net,html,declarative,C#,Asp.net,Html,Declarative,有人能告诉我,在ASP.NET页面中动态添加HTML内容的“正确”方法是什么吗 我知道以下声明性方法 //Declaration <%= MyMethodCall() %> //And in the code behind. protected String MyMethodCall() { return "Test Value"; } //声明 //在代码背后。 受保护的字符串MyMethodCall() { 返回“测试值”; } 有更好的或最佳实践方法吗 编辑:我

有人能告诉我,在ASP.NET页面中动态添加HTML内容的“正确”方法是什么吗

我知道以下声明性方法

//Declaration
<%= MyMethodCall() %>


//And in the code behind.
protected String MyMethodCall()
{
    return "Test Value";
}
//声明
//在代码背后。
受保护的字符串MyMethodCall()
{
返回“测试值”;
}
有更好的或最佳实践方法吗


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

取决于您要执行的操作

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

文字控制引用为

好吧,既然你想在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);
LiteralControl imageGallery=新建LiteralControl();
字符串divStart=@“
    ”; imageGallery.Text+=divStart; foreach([图像中的图像]) { 字符串imageHTML=@“
  • [字幕]
  • ”; imageGallery.Text+=imageHTML; } 字符串divEnd=@“
”; imageGallery.Text+=divEnd; 此[divOnPage].Controls.Add(imageGallery);
有几种方法可以做到这一点,具体使用哪种方法取决于您的场景和偏好

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

      <div id="DIV1" runat="server"></div>
      
      另一种选择

      //.aspx
      <asp:Literal ID="myText" runat="server"></asp:Literal>
      
      
      //.aspx.cs
      protected Literal myText;
      myText.Text = "Hello, World!";
      
      /.aspx
      //.aspx.cs
      受保护的文本;
      Text=“你好,世界!”;
      
      与我指定的方法相比,这种方法有哪些优点?PS:感谢您提供的全面示例!这个代码太满了。为什么不使用中继器呢?或.aspx中的一个for。连接字符串以创建HTML不是我的最佳做法。在C#code中组装标记应该是绝对的最后手段,因为它会造成一场维护噩梦,让Cthulhu感到自豪。至少,封装到用户或服务器控件中。是的,就字符串/HTML concat而言,您是对的,我只是快速地将该示例放在一起,以说明任何实质性内容的要点,例如“照片库”,我建议使用react之类的内容,但这个问题是从2011年开始的,对于快速和肮脏的内容,这个答案是一个很好的解决方案
      //.aspx
      <asp:Literal ID="myText" runat="server"></asp:Literal>
      
      
      //.aspx.cs
      protected Literal myText;
      myText.Text = "Hello, World!";