The DetailsView Server Control

detailsview

Image credit: commons.wikimedia.org

The DetailsView server control in asp.net provides us the flexibility to operate on the look-up tables in a very efficient way.Here,we are referring to the two tables of the Northwind database. One is Products table and another is Category table.
Some of the Products columns are displayed in GridView .
The other columns of our interest such as CategoryName and Description with respect to each Category in the Category table are displayed inside the DetailsView whenever a user selects a specific Product in the GridView.Here we go.

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”WebForm1.aspx.cs” Inherits=”DetailsViewDemo.WebForm1″ %>

<!DOCTYPE html>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:GridView ID=”GridView1″ runat=”server” OnSelectedIndexChanged=”GridView1_SelectedIndexChanged” BackColor=”White” BorderColor=”#CC9966″ BorderStyle=”None” BorderWidth=”1px” CellPadding=”4″ AllowPaging=”True” AutoGenerateColumns=”False” DataKeyNames=”ProductID” DataSourceID=”SqlDataSource1″ PageSize=”5″ OnPageIndexChanging=”GridView1_PageIndexChanging”>
<Columns>
<asp:CommandField ShowSelectButton=”True” />
<asp:BoundField DataField=”ProductID” HeaderText=”ProductID” ReadOnly=”True” SortExpression=”ProductID” />
<asp:BoundField DataField=”CategoryID” HeaderText=”CategoryID” SortExpression=”CategoryID” />
<asp:BoundField DataField=”ProductName” HeaderText=”ProductName” SortExpression=”ProductName” />
<asp:BoundField DataField=”QuantityPerUnit” HeaderText=”QuantityPerUnit” SortExpression=”QuantityPerUnit” />
<asp:BoundField DataField=”UnitPrice” HeaderText=”UnitPrice” SortExpression=”UnitPrice” />
<asp:BoundField DataField=”UnitsInStock” HeaderText=”UnitsInStock” SortExpression=”UnitsInStock” />
<asp:BoundField DataField=”UnitsOnOrder” HeaderText=”UnitsOnOrder” SortExpression=”UnitsOnOrder” />
</Columns>
<FooterStyle BackColor=”#FFFFCC” ForeColor=”#330099″ />
<HeaderStyle BackColor=”#990000″ Font-Bold=”True” ForeColor=”#FFFFCC” />
<PagerStyle BackColor=”#FFFFCC” ForeColor=”#330099″ HorizontalAlign=”Center” />
<RowStyle BackColor=”White” ForeColor=”#330099″ />
<SelectedRowStyle BackColor=”#FFCC66″ Font-Bold=”True” ForeColor=”#663399″ />
<SortedAscendingCellStyle BackColor=”#FEFCEB” />
<SortedAscendingHeaderStyle BackColor=”#AF0101″ />
<SortedDescendingCellStyle BackColor=”#F6F0C0″ />
<SortedDescendingHeaderStyle BackColor=”#7E0000″ />
</asp:GridView>
<br />
<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:NORTHWNDConnectionString %>” SelectCommand=”SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder] , [CategoryID] FROM [Alphabetical list of products]”></asp:SqlDataSource>

<br />
<asp:DetailsView ID=”DetailsView1″ runat=”server” Height=”50px” Width=”125px” BackColor=”White” BorderColor=”#3366CC” BorderStyle=”None” BorderWidth=”1px” CellPadding=”4″ OnPageIndexChanging=”DetailsView1_PageIndexChanging” DataSourceID=”SqlDataSource2″ AutoGenerateRows=”False” AllowPaging=”True”>
<EditRowStyle BackColor=”#009999″ Font-Bold=”True” ForeColor=”#CCFF99″ />
<Fields>
<asp:BoundField DataField=”CategoryName” HeaderText=”CategoryName” SortExpression=”CategoryName” />
<asp:BoundField DataField=”Description” HeaderText=”Description” SortExpression=”Description” />
</Fields>
<FooterStyle BackColor=”#99CCCC” ForeColor=”#003399″ />
<HeaderStyle BackColor=”#003399″ Font-Bold=”True” ForeColor=”#CCCCFF” />
<PagerStyle BackColor=”#99CCCC” ForeColor=”#003399″ HorizontalAlign=”Left” />
<RowStyle BackColor=”White” ForeColor=”#003399″ />
</asp:DetailsView>

<br />
<asp:SqlDataSource ID=”SqlDataSource2″ runat=”server” ConnectionString=”<%$ ConnectionStrings:NORTHWNDConnectionString %>” SelectCommand=”SELECT [CategoryName], [Description] FROM [Categories] WHERE ([CategoryID] = @CategoryID)”>
<SelectParameters>
<asp:ControlParameter ControlID=”GridView1″ Name=”CategoryID” PropertyName=”SelectedValue” Type=”Int32″ />
</SelectParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

The Code-behind is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DetailsViewDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
DetailsView1.DataBind();
}

protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{

}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
DetailsView1.DataBind();

}
}
}

This asp.net article best describes how to use DataList and Repeater control in our asp.net application. Here is the link

Syed Adeel Ahmed
Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.