Here is the code for populating a DropDown based on selection of another drop down list in Details view control of ASP .NET , in this example i've added two DropDowns in DetailsView control using TemplateField and InsertItemTemplate
The Second DropDown (ddlProducts) is getting populated based on Category selected in Category DropDown , For this i've used SelectedIndexChanged event and findControl method to find the control and sqldataSource is used as datasource for the dropdowns and DetailView
Download source code for Populating dropdown based on the selection of first drop down in DetailsView using FindControl and ItemTemplate
Here is the html source
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="dView" runat="server"
AutoGenerateRows="False"
DataSourceID="SqlDataSourceDetails"
DefaultMode="Insert"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="ID"
HeaderText="ID" SortExpression="ID" />
<asp:TemplateField HeaderText="Category">
<InsertItemTemplate>
<asp:DropDownList ID="ddlCategory" runat="server"
AutoPostBack="true"
DataSourceID="SqlDataSourceCategory"
DataTextField="Category"
DataValueField="Category"
OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceCategory"
runat="server"
ConnectionString="<%$ ConnectionStrings:dvConnectionString %>"
SelectCommand="SELECT DISTINCT [Category] FROM [Products]">
</asp:SqlDataSource>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<InsertItemTemplate>
<asp:DropDownList ID="ddlProducts"
runat="server"
DataSourceID="SqlDataSourceProd"
DataTextField="Product"
DataValueField="Product">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceProd"
runat="server"
ConnectionString="<%$ ConnectionStrings:dvConnectionString %>"
SelectCommand="SELECT [Product] FROM [Products] WHERE ([Category] = @Category)"
OnSelecting="SqlDataSourceProd_Selecting">
<SelectParameters>
<asp:Parameter Name="Category" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</InsertItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Product"
HeaderText="Product"
SortExpression="Product" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSourceDetails"
runat="server"
ConnectionString="<%$ ConnectionStrings:dvConnectionString %>"
SelectCommand="SELECT * FROM [Products]" ></asp:SqlDataSource>
</div>
</form>
</body>
</html>
And the code behind for this
protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlProducts = (DropDownList)dView.FindControl("ddlProducts");
if (ddlProducts != null)
{
ddlProducts.DataBind();
}
}
protected void SqlDataSourceProd_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
DropDownList ddlCategory = (DropDownList)dView.FindControl("ddlCategory");
if (ddlCategory != null)
{
e.Command.Parameters["@Category"].Value = ddlCategory.SelectedValue;
}
Conclusion
Download the sample code attached
http://www.box.net/shared/47kyv5m3fv

About the Author