自己封裝了一個gridview控制項,在製作分頁功能時出了一個錯誤,比較有意思。
這是錯誤碼,在gridView執行DataBind函數的時候。
at System.Web.UI.WebControls.GridView.BuildCallbackArgument(Int32 pageIndex)
at System.Web.UI.WebControls.GridView.CreateNumericPager(TableRow row, PagedDataSource pagedDataSource, Boolean
addFirstLastPageButtons)
at System.Web.UI.WebControls.GridView.InitializePager(GridViewRow row, Int32 columnSpan, PagedDataSource
pagedDataSource)
at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType
rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields,
TableRowCollection rows, PagedDataSource pagedDataSource)
at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback
callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.GridView.DataBind()
at Maxplatform.UI.Web.Controls.EntityGrid.BindData(Object parent) in
看上去,好像是在建立頁號的時候出錯了。提示是“未將對象應用到執行個體”。
使用這個控制項時的代碼
EntityGrid grid = new EntityGrid();
grid.BindData(this);
cell.Controls.Add(grid);
但是在測試時,gridview分頁沒有問題,問題出在哪裡呢?
這是運行正確時gridview產生的html代碼,它使用js執行callback
<a href="javascript:__doPostBack('ctl01$cph_webpart_manager$WPM$VE_viewplace$view_common_grid$ctl13$ctl02','')">第2頁</a>
js:__doPostBack函數是使用控制項的路徑來callback的:'ctl01$cph_webpart_manager$WPM$VE_viewplace$view_common_grid$ctl13$ctl02'。這就是關鍵的地方。
前一個出錯就是出在這裡:路徑有錯誤。因為這兩句代碼的緣故:
grid.BindData(this);
cell.Controls.Add(grid);
也就是,現綁定,在把控制項載入到父控制項上時,js構造路徑出錯。
反過來這樣處理 :
cell.Controls.Add(grid);
grid.BindData(this);
也就是先放置控制項,確定它的路徑,然後再綁定,讓js產生callback的代碼,結果,剛才的異常消失了。
關鍵的問題是:
gridview是在databind()中立即用來構建js回調的路徑。
文章來源:http://blog.aspcool.com/dlwang2002/archive/2005/08/05/2615.html