Introduction:
Now in this
article i will explain how to fill CheckBoxList based on DropDownList selection
i.e. whenever  an item from the DropDownList is selected corresponding
Course List will get filled in CheckBoxList. This is very common requirement while working on asp.net application.
Implementation: Let's create an asp.net application to understand
 In the design page(.aspx) place a
     DropDownList and a CheckBoxList control as:
<fieldset style="width:300px">
            <legend>Fill CheckBoxList On Dropdown Selection</legend>
         <br /><br /> <asp:DropDownList ID="ddlDept" runat="server"
            onselectedindexchanged="
ddlDept_SelectedIndexChanged" AutoPostBack="true">
        </asp:DropDownList>
        <br />
        <br />
        <asp:CheckBoxList ID="cblEmployee"
runat="server"
RepeatColumns="2">
        </asp:CheckBoxList>
       </fieldset>
First we need to create sql server database
     e.g. "MyDataBase" and in that create two tables as:
Create a table Dept_table as:
-  Create a connectionstring in the web.config
     file under configuration tag as:
<configuration>
<connectionStrings>
                                <add name="EmpCon" connectionString="Data Source=localhost;Initial
Catalog=Test;Integrated Security=True"/>
</connectionStrings>
</configuration>
Now in the code behind file(.aspx.cs) write the code as
 C#.Net
Code to Fill
CheckBoxList based on DropDownList selection in asp.net
 First include following namespaces :
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
protected void Page_Load(object sender, EventArgs
e)
    {
        if (!Page.IsPostBack)
        {
            FillQualDropdownList();
        }
    }
    private void
FillQualDropdownList()
    {
        SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["EmpCon"].ConnectionString);
        SqlCommand cmd = new
SqlCommand("Select
* from Dept_Table", con);
        SqlDataAdapter adp = new
SqlDataAdapter(cmd);
        DataTable dt = new
DataTable();
       
adp.Fill(dt);
       
ddlDept.DataSource = dt;
       
ddlDept.DataTextField = "Dept_Name";
       
ddlDept.DataValueField = "Dept_Id_Pk";
       
ddlDept.DataBind();
       
ddlDept.Items.Insert(0, "Select
Department");
    }
    protected void
ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new
DataTable();
        if (ddlDept.SelectedIndex != 0)
        {
            SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["Empcon"].ConnectionString);
            SqlCommand cmd = new
SqlCommand("SELECT
Emp_Table.Emp_Name, Emp_Table.Emp_Id_Pk FROM Emp_Table INNER JOIN Dept_Table ON
Emp_Table.Dept_Id_Fk = Dept_Table.Dept_Id_Pk WHERE (Emp_Table.Dept_Id_Fk =
" + ddlDept.SelectedValue + ")",
con);
            SqlDataAdapter adp = new
SqlDataAdapter(cmd);
           
adp.Fill(dt);
           
cblEmployee.DataSource = dt;
           
cblEmployee.DataTextField = "Emp_Name";
           
cblEmployee.DataValueField = "Emp_Id_Pk";
           
cblEmployee.DataBind();
        }
        else
        {
           
cblEmployee.Items.Clear();
           
cblEmployee.DataSource = null;
           
cblEmployee.DataBind();
        }
    }
}
VB.Net
Code to Fill
CheckBoxList based on DropDownList selection in asp.net
- Now in the code behind file(.aspx.vb) write the code as
First
import the following namespaces :
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
 
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            FillQualDropdownList()
        End If
    End Sub
    Private Sub FillQualDropdownList()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("EmpCon").ConnectionString)
Dim cmd As New SqlCommand("Select * from Dept_table", con)
Dim cmd As New SqlCommand("Select * from Dept_table", con)
        Dim adp As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        adp.Fill(dt)
        ddlDept.DataSource = dt
        ddlDept.DataTextField = "Dept_Name"
        ddlDept.DataValueField = "Dept_Id_Pk"
        ddlDept.DataBind()
        ddlDept.Items.Insert(0, "Select Department")
    End Sub
    Protected Sub ddlDept_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)Handles ddlDept_SelectedIndexChanged
        Dim dt As New DataTable()
        If ddlDept.SelectedIndex <> 0 Then
            Dim cmd As New SqlCommand("SELECT Emp_Table.Emp_Name, Emp_Table.Emp_Id_Pk FROM Emp_Table INNER JOIN Dept_Table ON Emp_Table.Dept_Id_Fk = Dept_Table.Dept_Id_Pk WHERE (Emp_Table.Dept_Id_Fk = " + ddlDept.SelectedValue + ")", con)
            Dim adp As New SqlDataAdapter(cmd)
            adp.Fill(dt)
            cblEmployee.DataSource = dt
            cblEmployee.DataTextField = "Emp_Name"
            cblEmployee.DataValueField = "Emp_Id_Pk"
            cblEmployee.DataBind()
        Else
            cblEmployee.Items.Clear()
            cblEmployee.DataSource = Nothing
            cblEmployee.DataBind()
        End If
    End Sub



 
 
 
 
 
 
 
 
 
0 comments:
Post a Comment