關於asp .net datagrid

來源:互聯網
上載者:User

the events of life circle in a datagrid

first, there is a important object in datagrid : datagriditem, we can get them use datagrid.items which is inherited from System.Web.UI.WebControls;

the datagriditem has itemtype with type of enum listitemtype and include header, pager, footer, item, alternatingItem,SelectedItem,EditItem,Separator

itemtype "item and alternatingItem" are very important cause we can reterive data from item with these two types which is bounding to the datasource.

at the same time , we can let a datagrid has our own datagriditems:

we should have a class which is inherited from System.Web.UI.WebControls.DataGridItem:

public class DataGridItem : System.Web.UI.WebControls.DataGridItem, IPostBackEventHandler    {        public DataGridItem(int itemIndex, int dataSetIndex, ListItemType itemType)            : base(itemIndex, dataSetIndex, itemType)        {        }        #region IPostBackEventHandler Members        public void RaisePostBackEvent(string eventArgument)        {            var commandArgs = new CommandEventArgs(eventArgument, null);            var args =                new DataGridCommandEventArgs(this, this, commandArgs);            RaiseBubbleEvent(this, args);        }
    public overrid void Render(HtmlTextWriter writer)
   {
        // code here can change the style of a datagrid.
   }
       #endregion    }

if you override the render method , we can change the style of a datagrid.

don't forget to use this DataGridItem , you should let your datagrid have your own datagriditem, then you can use it do whatever you want, you should override onitemcreate method in your datagrid


protected override System.Web.UI.WebControls.DataGridItem CreateItem(int itemIndex, int dataSourceIndex, ListItemType itemType)        {            switch (itemType)            {                case ListItemType.SelectedItem:                case ListItemType.AlternatingItem:                case ListItemType.Item:                return new DataGridItem(itemIndex, dataSourceIndex, itemType, new RMAFormPickupDetail(DataSet.CurrentTable.Rows[getDataSetIndex(dataSourceIndex)]));                default:                    return base.CreateItem(itemIndex, dataSourceIndex, itemType);            }        }

here , your datagrid has your own datagriditem.

the event of a datagrid.

datagrid.datasource = somedatasource;

datagrid.databind();

when this happens, the events happens as following sequence:

1.datagrid createitem()

2.template control in datagrid load()

3.template control in datagrid bind()

4.datagriditem render()

5.datagrid onitemdatabind() (a datagriditem render() as many times as a datagrid's datagriditem which's type is item or alternatingitem)

6.datagrid unload()

聯繫我們

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