1. 程式人生 > >GridView事件DataBinding,DataBound,RowCreated,RowDataBound區別及執行順序分析

GridView事件DataBinding,DataBound,RowCreated,RowDataBound區別及執行順序分析

嚴格的說,DataBinding,DataBound並不是GridView特有的事件,其他的控制元件諸如ListBox等也有DataBinding,DataBound事件。


DataBinding事件
MSDN解釋:Occurs when the server control binds to a data source.
This event notifies the server control to perform any data-binding logic that has been written for it.
譯為:該事件當伺服器控制元件繫結資料時發生。


DataBound事件
MSDN解釋:Occurs after the server control binds to a data source.
This event notifies the server control that any data binding logic written for it has completed.
譯為:該事件當伺服器控制元件完成資料繫結後發生。


RowCreated事件
MSDN解釋:Occurs when a row is created in a GridView control.
譯為:當GridView的行被建立時發生。

RowDataBound事件
MSDN解釋:Occurs when a data row is bound to data in a GridView control.
譯為:當GridView的行被繫結資料時發生。


四個事件的執行順序:
假定GridView有3行資料

aspx程式碼

cs程式碼(只含事件頭)

發生次序是:
1. DataBinding
2. RowCreated
3. RowDataBound
4. RowCreated
5. RowDataBound
6. RowCreated
7. RowDataBound
8. DataBound

GridView取得資料有3條,因此Row的相關事件執行3次。

另外,從字面本身也可知事件之間的區別

比如,DataBound,DataBinding兩個事件,單詞Bound是Bind的過去式,即DataBound是繫結事件完成後發生的。

還有諸如RowDeleted,RowDeleting,都可以從詞義本身的差別加以區分。