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
If you like this article, subscribe to our
RSS Feed. You can also
subscribe via email to our Interview Questions, Codes and Forums section.