Hi micah,
Refer below code.
HTML
<asp:DropDownList ID="ddlproductcategory" runat="server" OnSelectedIndexChanged="ddlproductcategory_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<asp:DropDownList ID="ddprice" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddiscount" runat="server">
</asp:DropDownList>
Namespaces
using System.Configuration;
using System.Data.SqlClient;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
string sql = "SELECT C_ID, Category FROM ItemCategory";
BindDropDownList(ddlproductcategory, sql, "Category", "C_ID", "Select Category");
ddprice.Enabled = false;
ddprice.Enabled = false;
ddiscount.Items.Insert(0, new ListItem("Select Price", "0"));
ddiscount.Items.Insert(0, new ListItem("Select Discount", "0"));
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
}
protected void ddlproductcategory_SelectedIndexChanged(object sender, EventArgs e)
{
ddprice.Enabled = false;
ddiscount.Enabled = false;
ddprice.Items.Clear();
ddiscount.Items.Clear();
ddprice.Items.Insert(0, new ListItem("Select Price", "0"));
ddiscount.Items.Insert(0, new ListItem("Select Discount", "0"));
int cid = int.Parse(ddlproductcategory.SelectedItem.Value);
if (cid > 0)
{
BindDropDownList(ddprice, string.Format("SELECT C_ID, Price FROM Itemprice where C_ID = {0}", cid), "Price", "C_ID", "Select Price");
ddprice.Enabled = true;
BindDropDownList(ddprice, string.Format("SELECT C_ID, Discount FROM Itemprice where C_ID = {0}", cid), "Discount", "C_ID", "Select Discount");
ddprice.Enabled = true;
}
}
private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
string conString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = text;
ddl.DataValueField = value;
ddl.DataBind();
con.Close();
}
}
ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}