c# - 用c#生成特定的html标签

标签 c# html asp.net

我有一个带有图片库的网站。我想将图片动态添加到图库中。我有这段代码:

string[] files = Directory.GetFiles(Server.MapPath(@"images\gallery\projects"));
string temp = files[0];

for (int i = 1; i < files.Length; i++)
{
    HyperLink hp = new HyperLink();

    hp.ImageUrl = "images/gallery/projects/" + Path.GetFileName(files[i]);
    hp.NavigateUrl = "images/gallery/projects/" + Path.GetFileName(temp);
    Panel1.Controls.Add(hp);
    temp = files[i];
}

用图片迭代文件夹并生成链接标签,但这不是我需要的。我正在尝试生成此标记:

<a href="images/gallery/picture_fullsize.jpg" 
   title="Caption for picture goes here">
    <img src="images/gallery/picture_thumbnail.jpg"/>
</a>

任何指示或解决方案将不胜感激。

最佳答案

您现有的方法很棒,它是强类型的,并且比您自己创建链接更不容易出错。

如果要给标签添加标题,可以使用Attributes.Add方法。

var hp = new HyperLink();
hp.ImageUrl = "images/gallery/projects/" + Path.GetFileName(files[i]);
hp.NavigateUrl = "images/gallery/projects/" + Path.GetFileName(temp);
hp.Attributes.Add("title", "Caption for picture goes here");
Panel1.Controls.Add(hp);

关于c# - 用c#生成特定的html标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19434148/

相关文章:

html - CSS 图像和伪元素过渡不同步

html - 保持背景图像全高,同时仅裁剪边以保持比例

html - 根据 div 高度,移动 View 上的字体大小不一致?

asp.net - 主域与受信任域之间的信任关系失败

c# - Count 或 Length 属性是否在每次调用时都计算?

C# - 最接近 0 的数字

c# - .NET UrlEncode 不工作?

c# - 如何在使用 asp.net 或 jquery 保存之前在文件上传控件中预览上传的图像?

c# - 如何以不同的格式格式化 DateTime?

c# - Json.net 在 MVC4 中使用循环引用反序列化 json