2013-05-17 56 views
0

我開始工作Asp.Net Mvc Html helpertagbuilder。我正在嘗試創建一個tagbuilder的表格,當我看到源代碼時,會創建表格,但不會創建trtd。我錯在哪裏?tagbuilder沒有顯示在源

public static HtmlString TbTile(this HtmlHelper helper) 
      { 
       TagBuilder tagBuilder = new TagBuilder("table"); 
       tagBuilder.Attributes["style"] = "background-color:red;height:200px;"; 
       tagBuilder.Attributes["width"] = "100%"; 
       tagBuilder.Attributes["border"] = "1"; 
       TagBuilder tbody = new TagBuilder("tbody"); 
       TagBuilder tr = new TagBuilder("tr"); 
       TagBuilder td = new TagBuilder("td"); 
       TagBuilder th = new TagBuilder("th"); 
       td.Attributes["style"] = "background-color:green;height:100px;"; 
       td.Attributes["height"] = "50%"; 
       td.InnerHtml = "test td1"; 
       return new HtmlString(tagBuilder.ToString()); 
      } 


@Html.TbTile() 

回答

1

你錯過了,你應該把TD INTU TR和TR到表的步驟。 試試這個:

public static HtmlString TbTile(this HtmlHelper helper) 
    { 
     TagBuilder tagBuilder = new TagBuilder("table"); 
     tagBuilder.Attributes["style"] = "background-color:red;height:200px;"; 
     tagBuilder.Attributes["width"] = "100%"; 
     tagBuilder.Attributes["border"] = "1"; 
     TagBuilder tbody = new TagBuilder("tbody"); 
     TagBuilder tr = new TagBuilder("tr"); 
     TagBuilder td = new TagBuilder("td"); 
     TagBuilder th = new TagBuilder("th"); 
     td.Attributes["style"] = "background-color:green;height:100px;"; 
     td.Attributes["height"] = "50%"; 
     td.InnerHtml = "test td1"; 
     /*missed*/ 
     tr.InnerHtml = td.ToString(); 
     tagBuilder.InnerHtml = tr.ToString(); 
     /**/ 
     return new HtmlString(tagBuilder.ToString()); 
    } 
+0

完美。萬分感謝。 – Kurkula

+0

Jarvan,如果tr中有3個tds,tr.innerHtml將如何顯示? – Kurkula

+0

​​AA​​BB​​CC。我不知道什麼是貴點,3個TDS在TR,tr.innerHtml肯定會在3個TDS是不是? – jarvanJiang