Dotnet:Select One Dropdown list based on another dropdown list from Database

Aspx code:

<%@ Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="json.About" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<tr>
<td>Hospital:</td>
<td><asp:DropDownList ID="ddlHospitals" runat="server" AutoPostBack = "true" OnSelectedIndexChanged = "Hospital_Changed">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Department:</td>
<td>
<asp:DropDownList ID="ddlDepartments" runat="server" AutoPostBack = "true" OnSelectedIndexChanged = "Department_Changed">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Doctor:</td>
<td>
<asp:DropDownList ID="ddlDoctors" runat="server">
</asp:DropDownList>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

 

C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace json
{
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = "select hospitalId,hospitalName from CheckList";
BindDropDownList(ddlHospitals, query, "HospitalName", "HospitalId", "Select Hospital");
ddlDepartments.Enabled = false;
ddlDoctors.Enabled = false;
ddlDepartments.Items.Insert(0, new ListItem("Select Department", "0"));
ddlDoctors.Items.Insert(0, new ListItem("Select Doctor", "0"));
}
}
private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = text;
ddl.DataValueField = value;
ddl.DataBind();
RemoveDuplicateItems(ddl);
con.Close();
}
ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}
void RemoveDuplicateItems(DropDownList ddl)
{
for (int i = 0; i < ddl.Items.Count; i++)
{
ddl.SelectedIndex = i;
string str = ddl.SelectedItem.ToString();
for (int counter = i + 1; counter < ddl.Items.Count; counter++)
{
ddl.SelectedIndex = counter;
string compareStr = ddl.SelectedItem.ToString();
if (str == compareStr)
{
ddl.Items.RemoveAt(counter);
counter = counter - 1;
}
}
}
ddl.SelectedIndex = 0;
}
protected void Hospital_Changed(object sender, EventArgs e)
{
ddlDepartments.Enabled = false;
ddlDoctors.Enabled = false;
ddlDepartments.Items.Clear();
ddlDoctors.Items.Clear();
ddlDepartments.Items.Insert(0, new ListItem("Select Department", "0"));
ddlDoctors.Items.Insert(0, new ListItem("Select Doctor", "0"));
int countryId = int.Parse(ddlHospitals.SelectedItem.Value);
if (countryId > 0)
{
string query = string.Format("select hospitalId, department from CheckList where hospitalId = {0}", countryId);
BindDropDownList(ddlDepartments, query, "Department", "HospitalId", "Select Department");
RemoveDuplicateItems(ddlDepartments);
ddlDepartments.Enabled = true;
}
}
protected void Department_Changed(object sender, EventArgs e)
{
ddlDoctors.Enabled = false;
ddlDoctors.Items.Clear();
ddlDoctors.Items.Insert(0, new ListItem("Select Doctor", "0"));
int stateId = int.Parse(ddlDepartments.SelectedItem.Value);
if (stateId > 0)
{
string query = string.Format("select hospitalId, doctorName from CheckList where hospitalId = {0}", stateId);
BindDropDownList(ddlDoctors, query, "DoctorName", "HospitalId", "Select Doctor");
ddlDoctors.Enabled = true;
}
}
}
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *