Go to DotNetFunda.com
 Welcome, Guest!  
LoginLogin  
Take a break and be productive! read technical jokes.
 Skip Navigation Links Home > Articles > ASP.NET > Populating dropdown based on the selection of first drop down in DetailsView using FindControl and ItemTemplate

All Articles | Submit Article |

Populating dropdown based on the selection of first drop down in DetailsView using FindControl and ItemTemplate

 Download source file
 Posted on: 12/30/2008 12:56:10 AM by Amit.jain | Views: 1150 | Category: ASP.NET | Level: Intermediate | Print Article
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

 Get Career Counseling  
DotNetFunda.Com brings you a FREE Career Counseling section where you can ask any type of career related question. Ask now!

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

Interesting?   Bookmark and Share


About amiT jaiN

Experience:4 year(s)
Home page:http://csharpdotnetfreak.blogspot.com/
Member since:Friday, December 26, 2008
Biography:
 Latest post(s) from Amit.jain

   ◘ Disabling copy paste in a textbox on aspx page posted on 1/22/2009 8:21:06 AM
   ◘ Creating AutoComplete TextBox in Windows Froms using C# posted on 1/13/2009 6:45:30 AM
   ◘ Implementing autocomplete textbox in gridview using Ajax autocomplete extender posted on 1/11/2009 10:05:43 AM
   ◘ Implementing search in GridView and highlight results posted on 1/8/2009 5:22:39 AM
   ◘ Redirection to Login page after session time out posted on 1/5/2009 7:49:14 AM


 Submit Article

About Us | Contact Us | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)