2012-04-11 43 views
0

我有一個GridView綁定到一個ObjectDataSource,包括一個複選框來選擇網格中的一個項目。從gridview獲取底層的ObjectDataSource

在CheckChanged事件我有一些像這樣的代碼:

 //Clear the existing selected row 
     foreach (GridViewRow oldrow in uxRaceList.Rows) 
     { 
      var otherOpt = (RadioButton)oldrow.FindControl("rdbRaceNum"); 
      if (otherOpt != sender) 
       otherOpt.Checked = false; 
     } 

     //Set the new selected row 
     RadioButton rb = (RadioButton)sender; 
     GridViewRow row = (GridViewRow)rb.NamingContainer; 
     ((RadioButton)row.FindControl("rdbRaceNum")).Checked = true; 

現在,我有到GridViewRow參考,是否有可能,我讓我的OrigionalDataSourceObject?

我知道我可以在網格中顯示的數據得到:

_selectedRaceNum = Convert.ToInt32(rb.Text.Substring(0, 1)); 

但我想是這樣的:

VAR odsMyobject = row.DataItem爲MyCustomObject;

我知道我可以在網格中存儲一個ID並使用它回顧我的數據庫以獲取數據,但我希望避免數據的其他往返。

也許我可以以某種方式問ObjectDataSource的對象?

謝謝。

回答

0

您的問題 -現在,我有到GridViewRow參考,有可能是我 在我的OrigionalDataSourceObject得到什麼?

GridViewDataSource ObjectDisposedPage Life Cycle結束。一旦Page被加載,您可以在Postback期間在Quick Watch中檢查它。


您的問題 -我知道我可以存儲ID詮釋他的網格,並用它來回頭看看我的 數據庫獲取數據,但我想避免再次往返於 數據。

Postback期間,您可以選擇在ViewState/Session之間保存數據。如果只是將數據保留在同一頁面中,則只應考慮ViewState


您的問題 -但我想是這樣的:

VAR odsMyobject = row.DataItem爲MyCustomObject;

您可以使用GridViewDataKeys,你可以很容易地使用您已使用NamingContainer已經計算了RowIndex訪問它。另一種選擇/替代方法是隱藏控制,並使用Property將其與對齊,並訪問此控件,如下所示。

((ControlType)row.FindControl("ControlID")). 
+0

謝謝。我誤解了DataSource對象會存在多長時間。 – Steve 2012-04-11 17:12:36