ASP.NET GridView Tutorial
Lesson 186 of 301 · Using objectdatasource control with detailsview – Part 38 · Database
Using objectdatasource control with detailsview – Part 38
Link for csharp, asp.net, ado.net, dotnet basics and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists
Link for text version of this video
http://csharp-video-tutorials.blogspot.com/2013/03/using-objectdatasource-control-with.html
In Part 37 of asp.net gridview tutorial, we discussed about using sqldatasource control with detailsview. In this video, we will discuss about using objectdatasource. We will be using tblEmployee table for this demo. If you need sql script to create this table, please refere to Part 37.
http://csharp-video-tutorials.blogspot.com/2013/03/detailsview-in-aspnet-part-37.html
In a gridview control, I just want to show 3 columns (Id, FirstName and City) from tblEmployee table. As soon as I select a row, in the gridview control, then all the columns of the selected row, should be displayed in details view control.
To use objectdatasource controls, to retrieve employee data, we need to create employee data access layer.
Step 1: Implement EmployeeDataAccessLayer.cs.
Step 2: Build your solution, so that the employee data access layer class is compiled.
Step 3: Drag and drop a gridview and a detailsview control on webform1.aspx
Step 4: Drag and drop 2 objectdatasource controls
Step 5: Configure ObjectDataSource1 control to retrieve data from EmployeeDataAccessLayer. Use GetAllEmployeesBasicDetails() as the SELECT method.
Step 6: Asscociate ObjectDataSource1 control with Gridview1 control.
Step 7: Configure ObjectDataSource2 control to retrieve data from EmployeeDataAccessLayer. Use GetAllEmployeesFullDetailsById(int Id) as the SELECT method. The value for the parameter – Id should come fromt the selected row in GridView1 control.
Step 8: Associate ObjectDataSource2 with DetailsView1 control.
Step 9: Flip the webform to HTML source mode and set
GridView1 DataKeyNames property to “Id”
Now set the colour scheme of GridView1 and DetailsView1 to BrownSugar and run the application. DetailsView is rendered without any employee data. This is because we don’t have any employee row selected in GridView1 intially. To correct this issue copy and paste the following code in webform1.aspx.cs
protected void Page_PreRender(object sender, EventArgs e)
{
if (GridView1.SelectedRow == null)
{
DetailsView1.Visible = false;
}
else
{
DetailsView1.Visible = true;
}
}