I’ve got a gridview bound to an ObjectDataSource including a checkbox to select an item in the grid.
In the CheckChanged event I’ve got some code like this:
//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;
Now that I have a reference to the GridViewRow, is it possible for me to get at my OrigionalDataSourceObject?
I know I can get at the data displayed in the gird:
_selectedRaceNum = Convert.ToInt32(rb.Text.Substring(0, 1));
But I want something like:
var odsMyobject = row.DataItem as MyCustomObject;
I know I can store an ID int he grid and use that to look back at my database to get the data, but I want to avoid another roundtrip to the data.
Perhaps I can somehow ask the ObjectDataSource for the object?
Thanks.
You
GridViewDataSource Objectwill beDisposedby End of thePage Life Cycle. You can check it in yourQuick WatchduringPostbackonce thePageis Loaded.You have an Option to keep the data in
ViewState/Sessionduring thePostback. In case it’s about keeping the data in the same page only, then onlyViewStateshould be considered.You can use
DataKeysinGridViewand you can easily access it using theRowIndexwhich you have already calculated usingNamingContainer. Another Option/Alternative is to Hide the Control andBindit with yourPropertyand Access this Control like below.