我有这个
foreach (var columnName in columns)
{
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.AddAttribute("href", null);
writer.Write("Delete");
writer.RenderEndTag();
}
当我在我的html帮助器类中使用此方法时,我根据字符串[]列参数中的列数来完成此for循环。它第一次出现我得到了这个
<a>Delete</a>
2nd time it goes around
<a href="">Delete</a>
3rd time I get
<a href="">Delete</a>
and so on.
为什么第一个错过了“href”?我不明白。
作者的另一件事也是作为参数传递的。
这是一个控制台应用。我只是快速地扔在一起
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var writer = new HtmlTextWriter(new StringWriter());
string[] columns = new string[4];
columns[0] = "hi";
columns[1] = "bye";
columns[2] = "hi";
columns[3] = "bye";
foreach (var columnName in columns)
{
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.AddAttribute("href", "g");
writer.Write("Delete");
writer.RenderEndTag();
}
Console.WriteLine(writer.InnerWriter.ToString());
}
}
}
答案 0 :(得分:3)
更改陈述的顺序:
writer.AddAttribute("href", "g");
writer.RenderBeginTag(HtmlTextWriterTag.A);