1. 程式人生 > >GridView 控制元件事件發生順序

GridView 控制元件事件發生順序

GridView顯示繫結的資料(預設為5行):
DataBinding
RowCreated:Header[0]
RowDataBound
RowCreated:DataRow[1]
RowDataBound
RowCreated:DataRow[2]
RowDataBound
RowCreated:DataRow[3]
RowDataBound
RowCreated:DataRow[4]
RowDataBound
RowCreated:DataRow[5]
RowDataBound
RowCreated:Footer[6] //不管有沒有頁角行,該事件都會發生
RowDataBound
RowCreated:Pager[7]
RowDataBound
DataBound

順序如下:
DataBinding
RowCreated
RowDataBound
......
DataBound
二、GridView點選分頁按鈕時的事件發生順序:
RowCommand

PageIndexChanging
PageIndexChanged
DataBinding
RowCreated:Header[8]
RowDataBound
RowCreated:DataRow[9]
RowDataBound
RowCreated:DataRow[10]
RowDataBound
RowCreated:DataRow[11]
RowDataBound
RowCreated:DataRow[12]
RowDataBound
RowCreated:DataRow[13]
RowDataBound
RowCreated:Footer[14]
RowDataBound
RowCreated:Pager[15]

RowDataBound
DataBound

總結:
在GridView繫結資料開始,也就是觸發DataBounding事件後,這時已經把資料從資料來源取出或者說已經規定了那些資料欄位將會被取出,其中 的Visible屬性為false的欄位的值是不會從資料來源中獲取的。這就是為什麼當GridView使用直接在html中將Visible=false 來隱藏某一列時,會得不到該列的值。然後呼叫RowCreated事件把一行資料全部填充到一個gridViewRow裡面,然後觸發 RowDataBound事件把該行繫結到GridView的Rows中。一直到最後觸發GridView的DataBound來繫結顯示資料。