0
Hi Aditya,
Try this...
Create table TVOP1(Productid varchar(50),Productname varchar(50))
Create table TVOP2(Productsubid varchar(50),Productsubname varchar(50),Productid varchar(50))
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Cascading_dropdownlist._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:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="Productpanel" runat="server">
<ContentTemplate >
<asp:Label ID="Label1" runat="server" Text="Choose Product" BackColor="#FFFF99"
ForeColor="Red" Width="150px"></asp:Label>
<asp:DropDownList ID="ddlProduct" AutoPostBack ="true"
AppendDataBoundItems="true" runat="server" Height="20px" Width="156px"
Font-Bold="True"
ForeColor="#FF9900"
onselectedindexchanged="ddlProduct_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID ="ddlProduct" />
</Triggers>
</asp:UpdatePanel>
<br />
<asp:UpdatePanel ID="Productsubpanel" runat="server">
<ContentTemplate >
<asp:Label ID="Label2" runat="server" Text="Choose Productsub" BackColor="#FFFF66"
ForeColor="Red" Width="150px"></asp:Label>
<asp:DropDownList ID="ddlProductsub" AutoPostBack ="true"
AppendDataBoundItems ="true" runat="server" Height="20px"
Width="155px" Font-Bold="True"
ForeColor="#993300">
</asp:DropDownList>
</ContentTemplate>
<Triggers >
<asp:AsyncPostBackTrigger ControlID ="ddlProductsub" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Cascading_dropdownlist
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
product();
}
}
void product()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select Productid,Productname from TVOP1";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "TVOP1");
ddlProduct.Items.Clear();
ddlProduct.Items.Add("--Select--");
ddlProduct.DataValueField = "Productid";
ddlProduct.DataTextField = "Productname";
ddlProduct.DataSource = ds;
ddlProduct.DataMember = "TVOP1";
ddlProduct.DataBind();
con.Close();
}
protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
{
productsub();
}
void productsub()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select Productsubid,Productsubname from TVOP2 where Productid='" + ddlProduct.SelectedValue + "'";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "TVOP2");
ddlProductsub.Items.Clear();
ddlProductsub.Items.Add("--Select--");
ddlProductsub.DataValueField = "Productsubid";
ddlProductsub.DataTextField = "Productsubname";
ddlProductsub.DataSource = ds;
ddlProductsub.DataMember = "TVOP2";
ddlProductsub.DataBind();
con.Close();
}
}
}
Thanks
If this post helps you mark it as answer
