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代码

  1. <asp:GridView   
  2. ID="GridViewMain"   
  3. runat="server"   
  4. AutoGenerateColumns="False"   
  5. OnDataBinding="GridViewMain_DataBinding"   
  6. OnDataBound="GridViewMain_DataBound"   
  7. OnRowCreated="GridViewMain_RowCreated"  
  8. OnRowDataBound="GridViewMain_RowDataBound"  
  9. >  

cs代码(只含事件头)

  1.     protected void GridViewMain_DataBinding(object sender,EventArgs e)  
  2.     {  
  3.   
  4.     }  
  5.   
  6.     protected void GridViewMain_DataBound  
  7. (object sender,EventArgs e)  
  8.     {  
  9.   
  10.     }  
  11.   
  12.     protected void GridViewMain_RowCreated(object sender, GridViewRowEventArgs e)  
  13.     {  
  14.   
  15.     }  
  16.   
  17.     protected void GridViewMain_RowDataBound(object sender, GridViewRowEventArgs e)  
  18.     {  
  19.   
  20.     }  

发生次序是:
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,都可以从词义本身的差别加以区分。

posted @ 2011-03-23 21:07  Jake.Xu  阅读(2176)  评论(0编辑  收藏  举报