GridView事件DataBinding,DataBound,RowCreated,RowDataBound区别及执行顺序分析
时间:2011-03-23 来源:Jake.Xu
严格的说,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代码
view plaincopy to clipboardprint?- <asp:GridView
- ID="GridViewMain"
- runat="server"
- AutoGenerateColumns="False"
- OnDataBinding="GridViewMain_DataBinding"
- OnDataBound="GridViewMain_DataBound"
- OnRowCreated="GridViewMain_RowCreated"
- OnRowDataBound="GridViewMain_RowDataBound"
- >
cs代码(只含事件头)
view plaincopy to clipboardprint?- protected void GridViewMain_DataBinding(object sender,EventArgs e)
- {
- }
- protected void GridViewMain_DataBound
- (object sender,EventArgs e)
- {
- }
- protected void GridViewMain_RowCreated(object sender, GridViewRowEventArgs e)
- {
- }
- protected void GridViewMain_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- }
发生次序是:
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,都可以从词义本身的差别加以区分。