2009-08-22 16 views
1

我有這個我在做什麼錯的HTMLWriter不寫屬性

foreach (var columnName in columns) 
{ 

    writer.RenderBeginTag(HtmlTextWriterTag.A); 
    writer.AddAttribute("href", null); 
    writer.Write("Delete"); 
    writer.RenderEndTag(); 
} 

當我到了這個方法在我的HTML輔助類,我做它通過這個for循環基礎上有多少列在string []列參數。第一次它得到這個

<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()); 
     } 
    } 
} 

回答

3

更改語句序列:

writer.AddAttribute("href", "g"); 
writer.RenderBeginTag(HtmlTextWriterTag.A);         
+0

不會有猜測。謝謝 – chobo2 2009-08-22 04:31:05

+0

是的,這有點奇怪,但很容易習慣。第一次使用HtmlWriter時,我偶然發現了這個問題。更糟的是,當你去使用HtmlGenericControl時,你首先創建'= new HtmlGenericControl(「a」);'然後*執行'.Attributes.Add(「href」,「g」)'(如果你是創建一個標籤的通用控制。 – Joshua 2009-08-22 05:19:17