Home > Forum > ASP.NET > Fill ListBox on DropDownList Selection

Fill ListBox on DropDownList Selection

by Thamil   on Aug 28, 2013   Category: ASP.NET  |  Views: 2357    |  Points: 5   |  Gold 
  Reply
Hi Everybody,
I have two controls, a ListBox and a DropdownList. DropdownList contains "Semesters" from database, and ListBox will contain "Courses" according to the value selected in Dropdownlist.
All I want is, when I select a value in Dropdownlist, the ListBox fills accordingly from the Database. i.e, if i select "1st Semester" in dropdownlist, all the courses/Subjects related to 1st Semester should be present in the ListBox.

I hope you people will understand my problem. kindly help me to solve this problem.

Question posted on behalf of Abdul.

Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :Fill ListBox on DropDownList Selection   
by Thamil
on Aug 28 2013 1:02AM
Points : 10
Gold 
Hello abdul

Please try with below code & this code will achieve your requirement.

public partial class WebForm35 : System.Web.UI.Page
{
private DataTable CourseDet
{
get
{
if (ViewState["Course"] != null)
return (DataTable)ViewState["Course"];
else
return null;
}
set
{
ViewState["Course"] = value;
}
}


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = LoadSemesterData();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Number";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
CourseDet = LoadCourseData();
ListCourse(DropDownList1.SelectedValue.ToString());
}
}
private DataTable LoadSemesterData()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("Number");

DataRow rw3 = dt.NewRow();
rw3["id"] = "1";
rw3["Number"] = "Semester-1";
dt.Rows.Add(rw3);

DataRow rw4 = dt.NewRow();
rw4["id"] = "2";
rw4["Number"] = "Semester-2";
dt.Rows.Add(rw4);

DataRow rw5 = dt.NewRow();
rw5["id"] = "3";
rw5["Number"] = "Semester-3";
dt.Rows.Add(rw5);

return dt;
}
private DataTable LoadCourseData()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("Course");

DataRow rw3 = dt.NewRow();
rw3["id"] = "1";
rw3["Course"] = "Course-1";
dt.Rows.Add(rw3);

DataRow rw4 = dt.NewRow();
rw4["id"] = "1";
rw4["Course"] = "Course-2";
dt.Rows.Add(rw4);

DataRow rw5 = dt.NewRow();
rw5["id"] = "2";
rw5["Course"] = "Course-3";
dt.Rows.Add(rw5);

DataRow rw6 = dt.NewRow();
rw6["id"] = "2";
rw6["Course"] = "Course-4";
dt.Rows.Add(rw6);

DataRow rw7 = dt.NewRow();
rw7["id"] = "3";
rw7["Course"] = "Course-5";
dt.Rows.Add(rw7);

DataRow rw8 = dt.NewRow();
rw8["id"] = "3";
rw8["Course"] = "Course-6";
dt.Rows.Add(rw8);

return dt;
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strSemNum = DropDownList1.SelectedValue.ToString();
ListCourse(strSemNum);
}
private void ListCourse(string SemNum)
{
DataTable dtCourse = CourseDet;
DataRow[] advRow = dtCourse.Select("id=" + SemNum);
DataTable newDataTable = advRow.CopyToDataTable<DataRow>();
ListBox1.DataSource = newDataTable;
ListBox1.DataTextField = "Course";
ListBox1.DataValueField = "id";
ListBox1.DataBind();
}
}

Recent Post

Latest Posts