Fill CheckBoxList based on
DropDownList selection
Create a table Qualification_Tb as:
Create a table Courses_Tb as:
.ASPX CODE:
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Fill
CheckBoxList based on DropDownList selection .aspx.cs" Inherits="Fill_CheckBoxList_based_on_DropDownList_selection_"
%>
<!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>Fill CheckBoxList based on DropDownList selection
</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlQual"
runat="server"
onselectedindexchanged="ddlQual_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
<br />
<br />
<asp:CheckBoxList ID="cblCourses"
runat="server"
RepeatColumns="2">
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
.CS 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;
public partial class Fill_CheckBoxList_based_on_DropDownList_selection_
: System.Web.UI.Page
{
string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void
Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillQualDropdownList();
}
}
private void
FillQualDropdownList()
{
SqlConnection con = new
SqlConnection(str);
SqlCommand cmd = new
SqlCommand("Select
* from Qualification_Tb", con);
SqlDataAdapter adp = new
SqlDataAdapter(cmd);
DataTable dt = new
DataTable();
adp.Fill(dt);
ddlQual.DataSource = dt;
ddlQual.DataTextField = "Qualification";
ddlQual.DataValueField = "Qualification_Id_Pk";
ddlQual.DataBind();
ddlQual.Items.Insert(0, "Select
Qualification");
//OR
ddlQual.Items.Insert(0, new ListItem("Select Qualification",
"-1"));
}
protected void
ddlQual_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = new
DataTable();
if (ddlQual.SelectedIndex != 0)
{
SqlConnection con = new
SqlConnection(str);
SqlCommand cmd = new
SqlCommand("SELECT
Courses_Tb.CourseName, Courses_Tb.Courses_Id_Pk FROM Courses_Tb INNER JOIN
Qualification_Tb ON Courses_Tb.Qualification_Id_Fk =
Qualification_Tb.Qualification_Id_Pk WHERE (Courses_Tb.Qualification_Id_Fk =
" + ddlQual.SelectedValue + ")",
con);
SqlDataAdapter adp = new
SqlDataAdapter(cmd);
adp.Fill(dt);
cblCourses.DataSource = dt;
cblCourses.DataTextField = "CourseName";
cblCourses.DataValueField = "Courses_Id_Pk";
cblCourses.DataBind();
}
else
{
cblCourses.Items.Clear();
cblCourses.DataSource = null;
cblCourses.DataBind();
}
}}
No comments:
Post a Comment