Go to DotNetFunda.com
 Online : 1386 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Populating dropdown based on the selection of first drop down in DetailsView using FindControl and ItemTemplate

  • Download the OOPS, ASP.NET and ADO.NET Training Videos for FREE, click here.

Submit Article | Articles Home | Search Articles |

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: 1857 | 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

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

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.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


About amiT jaiN

Experience:4 year(s)
Home page:http://csharpdotnetfreak.blogspot.com/
Member since:Friday, December 26, 2008
Level:Starter
Status: [Member]
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 | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
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)