C#-由後端程式加入Html code

 最近工作上遇到了瘋子,他把一大部分前端的東西都寫入後端。

說實在的明明有ASP.NET的designer 想做表格有dataview之類的

我不懂為什麼他想做如此瘋狂的事情,但是我還是寫出來這鬼東西了。


到後面MVC和Core都有類似的引入條件,拜託不要做一些奇怪的事情了


//第一種寫法

//建立table

Table table=new Table();                  //create one object of type Table

table.ID ="Table_Item";

table.Attributes .Add ("border","1px");  //自己加一個屬性

table.Style ["font-size"]="9pt";         //設定Style

table.Style ["width"]="100%";


//建立 Row (tr)

TableRow tRow = new TableRow();     //create a new object of type TableRow

table.Rows .Add (tRow);


//建立 Cell (td) 

TableCell tCell_0 = new TableCell(); 

tCell_0.Text =ii.ToString ();

tRow.Cells .Add (tCell_0); 


Label VV = new Label();

VV.Text ="第1欄";

 

TableCell tCell_1 = new TableCell();  //create a new object of type TableCell 

tCell_1.Width =50;

tCell_1.VerticalAlign =VerticalAlign.Top ;

tCell_1.Controls.Add (VV); 

tRow.Cells.Add(tCell_1);


//寫入 Page 前端

Page.Controls .Add (table);


//第二種寫法

//new HtmlGenericControl("html tag"); 內放html tag

HtmlGenericControl div = new HtmlGenericControl("div");

HtmlGenericControl ul = new HtmlGenericControl("ul");

//新增屬性

div.Attributes.Add("class", "row");

//新增入div中 <div> <ul> </ul> </div>

div.Controls.Add(ul);

//寫入 Html code 可以輸出成html樣貌

LiteralControl Literal_Content = new LiteralControl();

Literal_Content .Text = <p>Hello</p>


參考

https://blog.xuite.net/sunnysoap/r/16713313

留言

這個網誌中的熱門文章

無法載入檔案或組件 'System.IO.Compression' 或其相依性的其中之一。

MongoDB 入門

javascript 更改屬性及創建標籤