Asp.net後台建立HTML

來源:互聯網
上載者:User

  為了使HTML介面中的內容能根據資料庫中的內容動態顯示使用者需要的內容,或者根據許可權不同要顯示同而實現頁面內容的動態建立

  使用HtmlGenericControl建立HTML標籤

    引入命名空間: using System.Web.UI.HtmlControls;

    更改其屬性: hgg_div.Attributes.Add("style","width:200px; height:200px;");

    內容設定: hgg_div.InnerText = "我是一個" + htmlTag;(htmlTag可以是div,br,span…)

    或者InnerHtml來給div寫一些html

  使用Table newTable = new Table();建立表格控制項

    newTable.Width = 200;設定高

    newTable.Height = 200; 設定寬

  建立行: TableRow newTableRow = new TableRow();

      newTableRow.Height = 20;

  建立儲存格: TableCell newTableCell = new TableCell();

                      newTableCell.Width = 100;

        newTableCell.Text = "我是一個儲存格";

  添加到表格中: newTableRow.Controls.Add(newTableCell);

        newTableRow.Controls.Add(newTableCell);

        newTable.Controls.Add(newTableRow);

 

  將建立的標籤或者控制項添加到頁面中

    Page.Controls.Add(newTable);//添加到表單外(control)

    Page.Form.InnerHtml=str;//添加到表單內(html)

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 #region 引入命名空間 9 10 using System.Web.UI.HtmlControls;11 12 #endregion13 14 15 namespace Web_CreateHtml16 {17     public partial class _Default : System.Web.UI.Page18     {19         #region 表單載入20 21         protected void Page_Load(object sender, EventArgs e)22         {23             CreateHtmlTag("div");//建立一個div24             CreateTable();//建立一個Table控制項25             JointHtmlTag();//拼接一個div26         }27 28         #endregion29 30         #region 建立HtmlTag31 32         /// <summary>33         /// 建立Div,br,span等標籤通用方法34         /// </summary>35         private void CreateHtmlTag(string htmlTag)36         {37             HtmlGenericControl hgg_div = new HtmlGenericControl(htmlTag);38             hgg_div.Attributes.Add("style","width:200px; height:200px;");39             hgg_div.InnerText = "我是一個" + htmlTag;40             Page.Controls.Add(hgg_div);41         }42 43         #endregion44 45         #region 建立Table控制項46 47         /// <summary>48         /// 建立Table控制項49         /// </summary>50         private void CreateTable()51         {52             Table newTable = new Table();53             newTable.Width = 200;54             newTable.Height = 200;55             TableRow newTableRow = new TableRow();56             newTableRow.Height = 20;57             TableCell newTableCell = new TableCell();58             newTableCell.Width = 100;59             newTableCell.Text = "我是一個儲存格";60 61             newTableRow.Controls.Add(newTableCell);62             newTableRow.Controls.Add(newTableCell);63             newTable.Controls.Add(newTableRow);64             Page.Controls.Add(newTable);65         }66 67         #endregion68 69         #region 字串拼接HTML70 71         /// <summary>72         /// 字串拼接HTML73         /// </summary>74         private void JointHtmlTag()75         {76             string str = "<div style='width:200px;height:200px;'>我是拼接的div</div>";77             Page.Form.InnerHtml=str;78         }79 80         #endregion81     }82 }

最後附上源碼:Asp.net建立實現HTML標籤布局(table,div,br...).zip

 

作者:曾慶雷出處:http://www.cnblogs.com/zengqinglei本頁著作權歸作者和部落格園所有,歡迎轉載,但未經作者同意必須保留此段聲明, 且在文章頁面明顯位置給出原文連結,否則保留追究法律責任的權利
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.