ASP.NET WebForms Datacontrols : Dropdownlist and detailview connected.

Links for this article :

How to link a dropdownlist and a detailview in both ways ?

Ie: I want my dropdownlist to be populated with a list of vendors, and if I select one of them I want to see the details of the vendor in the detailview.
Plus, if I add/delete/update a vendor in the detailview, I want the dropdownlist to be updated.

This kind of questions is often asked on different forums, here is my solution:

Here are the steps to follow.

Step 1 : getting the datas

First we need a datasource, so for the demo we will create a simple vendor class in our DAL.

The vendor class has three properties : ID, Name and WebSite and one static method : “All” who allows us to retrieve a list of vendors.

Step 2 : managing the datas

We also need a manager class for these vendors, which will be in our BLL.

VendorsManager will contain the simple crud methods (Get, Insert, Update, Delete) and a “GetAll” method which simply call the DAL.Vendors’s “All” Method

Step 3 : displaying the data

Now that we have our datasource methods ready, we can create a webform named “DropDownDetailView.aspx”

The solution has to be like that:

In the aspx markup we’ll add an objectdatasource which will be used by the dropdownlist.
This objectdatasource will use the GetAll method from our BLL.


<asp:ObjectDataSource runat="server" ID="VendorsDataSource"
TypeName="DataControls.BLL.VendorsManager"
SelectMethod="GetAll"/>

Then we add the dropdownlist and configure it to use the objectdatasource.


<asp:DropDownList runat="server" ID="VendorsList"
DataSourceID="VendorsDataSource"
DataValueField="ID" DataTextField="Name" />

If you run the application you will have a dropdownlist populated with our vendors

Now we add a second objectdatasource with the same TypeName, and we set the SelectMethod, UpdateMethod, DeleteMethod and InsertMethod to use the methods of the VendorsManager’s methods.


<asp:ObjectDataSource runat="server" ID="VendorDetailDataSource"
TypeName="DataControls.BLL.VendorsManager"
SelectMethod="Get"
UpdateMethod="Update"
DeleteMethod="Delete"
InsertMethod="Insert"/>

And because the GetMethod takes a parameter, we will say to the objectdatasource to take this parameter from the Selected value of the dropdownlist.


<SelectParameters>
<asp:ControlParameter Name="id" ControlID="VendorsList" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>

So the complete objectdatasource will be


<asp:ObjectDataSource runat="server" ID="VendorDetailDataSource"
TypeName="DataControls.BLL.VendorsManager"
SelectMethod="Get"
UpdateMethod="Update"
DeleteMethod="Delete"
InsertMethod="Insert">
<SelectParameters>
<asp:ControlParameter Name="id" ControlID="VendorsList" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

Now that we have our datasource, we can link it to a detailview.
So we create the detailview and tell him to use the datasource


<asp:DetailsView runat="server" ID="VendorDetail"
DataSourceID="VendorDetailDataSource"  />

If we run the application, we see that the detailview is filled with the details of the first vendor

Try to select another vendor in the list -> nothing happens!
It’s because we don’t cause a postback to the server to let him change the data.

To achieve that, we simply set the AutoPostBack property of the dropdownlist to true, so the dropdownlist is


<asp:DropDownList runat="server" ID="VendorsList"
DataSourceID="VendorsDataSource"
DataValueField="ID" DataTextField="Name"
AutoPostBack="true"/>

Re-run the application and change the selected value of the dropdownlist.


Now the data in the detailview matches the selected vendor.

The first part of our need is achieved!

Step 4 : playing with the data

Now we want to be able to add, edit or delete vendors, so we set the detailview’s propertie AutoGenerateDeleteButton,  AutoGenerateEditButton and  AutoGenerateInsertButton to true.

And we don’t forget to set the DataKeyNames property to “ID”, because ID is the key for our vendors.

So now, the detailview looks like this:


<asp:DetailsView runat="server" ID="VendorDetail"
DataSourceID="VendorDetailDataSource"
DataKeyNames="ID"
AutoGenerateDeleteButton="true"
AutoGenerateEditButton="true"
AutoGenerateInsertButton="true"/>

Run the application and try to add a new item

Now that the new vendor is inserted, look at the values in the dropdownlist

Arghh, the new vendor is not in the list !

It’s normal! We don’t refresh the dropdownlist yet!
To do that, we’ll add handlers for deleted, inserted and updated events.

The aspx markup of the detailview becomes


<asp:DetailsView runat="server" ID="VendorDetail"
DataSourceID="VendorDetailDataSource"
DataKeyNames="ID"
AutoGenerateDeleteButton="true"
AutoGenerateEditButton="true"
AutoGenerateInsertButton="true"
onitemdeleted="VendorDetail_ItemDeleted"
oniteminserted="VendorDetail_ItemInserted"
onitemupdated="VendorDetail_ItemUpdated"/>

and in the code-behind of the page :


protected void VendorDetail_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
{
Refresh();
}

protected void VendorDetail_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
Refresh();
}

protected void VendorDetail_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
Refresh();
}

In each handler, I call  the Refresh() method , and in this method we will rebind the dropdownlist:


private void Refresh()
{
this.VendorsList.DataBind();
}

Now, run the application, and redo the same test:


Now look at the dropdownlist:

And that’s it! The new vendor is in the list

Now, all the steps are processed!

Here is the complete final aspx markup


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DropDownDetailView.aspx.cs" Inherits="DataControls.DropDownDetailView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>Dropdownlist and detailview demo</title>
</head>
<body>
 <form id="form1" runat="server">
 <asp:ObjectDataSource runat="server" ID="VendorsDataSource"
 TypeName="DataControls.BLL.VendorsManager"
 SelectMethod="GetAll"/>
 <asp:DropDownList runat="server" ID="VendorsList"
 DataSourceID="VendorsDataSource"
 DataValueField="ID" DataTextField="Name"
 AutoPostBack="true"/>

 <asp:ObjectDataSource runat="server" ID="VendorDetailDataSource"
 TypeName="DataControls.BLL.VendorsManager"
 SelectMethod="Get"
 UpdateMethod="Update"
 DeleteMethod="Delete"
 InsertMethod="Insert">
 <SelectParameters>
 <asp:ControlParameter Name="id" ControlID="VendorsList" PropertyName="SelectedValue" Type="Int32" />
 </SelectParameters>
 </asp:ObjectDataSource>
 <asp:DetailsView runat="server" ID="VendorDetail"
 DataSourceID="VendorDetailDataSource"
 DataKeyNames="ID"
 AutoGenerateDeleteButton="true"
 AutoGenerateEditButton="true"
 AutoGenerateInsertButton="true"
 onitemdeleted="VendorDetail_ItemDeleted"
 oniteminserted="VendorDetail_ItemInserted"
 onitemupdated="VendorDetail_ItemUpdated"/>
 </form>
</body>
</html>

And the matching code behind


using System.Web.UI.WebControls;

namespace DataControls
{
 public partial class DropDownDetailView : System.Web.UI.Page
 {
 protected void VendorDetail_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
 {
 Refresh();
 }

 protected void VendorDetail_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
 {
 Refresh();
 }

 protected void VendorDetail_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
 {
 Refresh();
 }

 private void Refresh()
 {
 this.VendorsList.DataBind();
 }
 }
}

Links for this article :

Incoming search terms:

  • link dropdownlist with detailsView
  • asp datacontrols
  • asp net search drop down list parameter
  • asp:detailsview change selected data
  • datacontrols asp
  • sharepoint 2010 typename objectdatasource selectparameters

About Sam Beauvois

Application Developer, .NET enthusiast since 2004, I'm interested in technology watch, usability, code quality, patterns & practices, UX, ...

1 comment

  1. Thanks mate, I applied this using sqldatasources and it worked just fine. It was the last step with the insert and update events that I was missing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>