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

Submit Article | Articles Home | Search Articles |

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

red flag  Posted on: 12/30/2008 12:56:10 AM by Amit.jain | Views: 2297 | Category: ASP.NET | Level: Intermediate


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


 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

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Found interesting? Add this to:

| More



Please Sign In to vote for this post.

 
Latest post(s) from Amit.jain

Latest Articles

About amiT jaiN

Experience:4 year(s)
Home page:http://csharpdotnetfreak.blogspot.com/
Member since:Friday, December 26, 2008
Level:Starter
Status: [Member]
Biography:

Submit Article

About Us | The Team | Advertise | Contact Us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 9/3/2010 4:27:51 AM