我有这个
foreach (var columnName in columns)
{
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.AddAttribute("href", null);
writer.Write("Delete");
writer.RenderEndTag();
}当我在我的html helper类中使用这个方法时,它会根据string[] columns参数中的列数经历这个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());
}
}
}发布于 2009-08-22 04:16:17
更改语句的顺序:
writer.AddAttribute("href", "g");
writer.RenderBeginTag(HtmlTextWriterTag.A); https://stackoverflow.com/questions/1315100
复制相似问题