Asp.Net伺服器控制項開發的Grid實現(二)

來源:互聯網
上載者:User

標籤:asp.net   grid   自訂控制項   伺服器控制項   

我們先來實現Grid類,代碼如下:

Grid.cs

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.UI;namespace AspNetServerControl{    [Designer("AspNetServerControl.Design.GridDesigner, AspNetServerControl.Design")]//設計時支援,需要另外寫一個對就的類    [ToolboxData("<{0}:Grid Title=\"Grid\" runat=\"server\"><Columns></Columns></{0}:Grid>")]    [ToolboxBitmap(typeof(Grid), "toolbox.Grid.bmp")]    [Description("表格控制項")]    [ParseChildren(true)]    [PersistChildren(false)]    [ControlBuilder(typeof(NotAllowWhitespaceLiteralsBuilder))]    public class Grid : ControlBase    {        #region 屬性        #region DataSource        private object _dataSource;        public object DataSource        {            get { return _dataSource; }            set { _dataSource = value; }        }        #endregion        #region Columns        private GridColumnCollection _columns;        /// <summary>        /// 列資料        /// </summary>        [Category(CategoryName.OPTIONS)]        [NotifyParentProperty(true)]        [PersistenceMode(PersistenceMode.InnerProperty)]        [Editor(typeof(GridColumnsEditor), typeof(System.Drawing.Design.UITypeEditor))]//用於編輯器        public virtual GridColumnCollection Columns        {            get            {                if (_columns == null)                {                    _columns = new GridColumnCollection(this);                }                return _columns;            }        }        #endregion        #endregion        protected override void Render(HtmlTextWriter writer)        {            base.Render(writer);            if (_columns == null)            {                return;            }            writer.Write("<table border=\"1\" cellspacing=\"0\">");            RenderHeader(writer);            RenderBody(writer);            writer.Write("</table>");        }        private void RenderHeader(HtmlTextWriter writer)        {            writer.Write("<tr>");            foreach (GridColumn column in Columns)            {                writer.Write(String.Format("<th  >{0}</th>", column.HeaderText));            }            writer.Write("</tr>");        }        private void RenderBody(HtmlTextWriter writer)        {            DataTable dt = DataSource as DataTable;            if (dt == null || dt.Rows.Count <= 0)            {                return;            }            writer.Write("<tbody>");            foreach (DataRow row in dt.Rows)            {                writer.Write("<tr >");                foreach (GridColumn column in Columns)                {                    writer.Write(String.Format("<td  >{0}</td>", row[column.DataField]));                }                writer.Write("</tr>");            }            writer.Write("</tbody>");        }    }}
1.Grid類前面的幾個屬性相當重要:

   (1).Designer是設計時支援,需要另外寫一個類庫來支援。

   (2).ParseChildren指示頁分析器應如何處理頁上聲明的伺服器控制項標記中嵌套的內容,也就是這個屬性決定了Grid內部還可以再嵌套其他內容(為true時才可嵌套),比如Columns。

2.繼承自ControlBase,該類後面會介紹。

3.DataSource屬性是資料來源。

4.Columns是Grid的內部列,其中[Editor(typeof(GridColumnsEditor), typeof(System.Drawing.Design.UITypeEditor))]這個特性非常重要,該特性指示了該屬性Columns可以在編輯中編輯。其中GridColumnsEditor是介面編輯的類,後面會介紹。

5.重載Render方法。該方法是將Grid最終html形式輸出。這裡以table的形式來實現Grid。

(1)將Columns中的所有列的列名以table中的th呈現

(2)將資料來源轉換成DataTable,然後遍曆每一行,並將每一行的資料以td的形式呈現。

下面來看一下ControlBase類。

ControlBase.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.UI;namespace AspNetServerControl{    public class ControlBase : Control, INamingContainer    {    }}
ControlBase類繼承自Control類及INamingContainer介面,這是自訂控制項所必須的。

再來看一下NotAllowWhitespaceLiteralsBuilder類

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.UI;namespace AspNetServerControl{    /// <summary>    /// 控制項內部不允許存在非標籤形式的字串    /// </summary>    internal class NotAllowWhitespaceLiteralsBuilder : ControlBuilder    {        /// <summary>        /// 不允許空白字元        /// </summary>        /// <returns></returns>        public override bool AllowWhitespaceLiterals()        {            return false;        }        /// <summary>        /// 忽略游離於標籤外的字串        /// </summary>        /// <param name="s"></param>        public override void AppendLiteralString(string s)        {        }        public override Type GetChildControlType(string tagName, System.Collections.IDictionary attribs)        {            return base.GetChildControlType(tagName, attribs);        }    }}
NotAllowWhitespaceLiteralsBuilder主要是用來控制控制項內部不允許存在非標籤形式的字串。

後面將會說明GridColumnsEditor及GridColumn欄位的定義,具體請看《Asp.Net伺服器控制項開發的Grid實現(二)》



Asp.Net伺服器控制項開發的Grid實現(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.