关于asp .net datagrid

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()

转载于:https://www.cnblogs.com/huangwj21/archive/2011/06/10/2077654.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值