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

來源:互聯網
上載者:User

標籤:.net   伺服器控制項   jquerymobile   grid   

下面是GridColumnsEditor的實現代碼:

GridColumnsEditor.cs

using System;using System.Collections.Generic;using System.ComponentModel.Design;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.UI.WebControls;namespace AspNetServerControl{    public class GridColumnsEditor : CollectionEditor    {        private Type[] types;        /// <summary>        /// 建構函式        /// </summary>        /// <param name="type">控制項類型</param>        public GridColumnsEditor(Type type)            : base(type)        {            types = new Type[] {                 typeof(BoundField)            };        }        /// <summary>        /// 擷取此集合編輯器可包含的資料類型        /// </summary>        /// <returns>類型集合</returns>        protected override Type[] CreateNewItemTypes()        {            return types;        }    }}
GridColumnsEditor繼承自CollectionEditor,CollectionEditor可以給使用者提供一個編輯的介面,並集合大部分的資料類型。

在建構函式中GridColumnsEditor(Type type)中,只實現了一個BoundField欄位,如果需要其他的欄位,可以在後面添加。比如

types = new Type[] {                 typeof(BoundField),typeof(CheckField)            };
下面看一下BoundField欄位的實現

 /// <summary>    /// 表格式資料繫結資料行    /// </summary>    [ToolboxItem(false)]    [ParseChildren(true)]    [PersistChildren(false)]    public class BoundField : GridColumn    {    }
BoundField繼承自GridColumn類,這裡也有一個ParseChildren屬性,主要是為了嵌套。

下面看一下GridColumn的實現

using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Text;using System.Web.UI;namespace AspNetServerControl{    /// <summary>    /// 表格列基類(抽象類別)    /// </summary>    [ToolboxItem(false)]    [ParseChildren(true)]    [PersistChildren(false)]    [DefaultProperty("HeaderText")]    public class GridColumn : ControlBase    {        private string _headerText = String.Empty;        /// <summary>        /// 標題列顯示的文字        /// </summary>        [Category(CategoryName.OPTIONS)]        [DefaultValue("")]        [Description("標題列顯示的文字")]        public string HeaderText        {            get            {                return _headerText;            }            set            {                _headerText = value;            }        }        private string _dataField = String.Empty;        /// <summary>        /// 欄位名稱        /// </summary>        [Category(CategoryName.OPTIONS)]        [DefaultValue("")]        [Description("欄位名稱")]        public string DataField        {            get            {                return _dataField;            }            set            {                _dataField = value;            }        }    }}
GridColumn也繼承自ControlBase,所以GridColumn其實也是一個控制項,只不過我們將其嵌套在了Grid中。

在Grid中定義Columns的屬性時,我們用的是GridColumnCollection類,而該類是一個GridColumn的集合,代碼如下。

public class GridColumnCollection : Collection<GridColumn>    {        public GridColumnCollection(ControlBase parent)        {        }    }

再看GridColumn類中,我們定義了HeaderText和DataField屬性,這兩個屬性就是我們在default.aspx頁面中編輯Grid時,給BoundField添加的屬性。

到此,整個Grid的封裝就算完成了。

如果結合jquerymobile,可以在Grid的Render函數中,依據jquerymobile的表格標記輸出。




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.