Please disable your adblock and script blockers to view this page
 

Fill CheckBoxList based on DropDownList selection in asp.net(C#, VB)

Dropdownlist and checkboxlist example in asp.netIntroduction: In previous articles i explained How to fill DropDownList from Sql server database in asp.net and How to Fill CheckBoxList from Sql Server table in asp.net(C#,VB) and "How to validate CheckBoxList using JavaScript in Asp.net(C#, VB)" .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 so i decided to post it so that other developers can take benefit from it.

Implementation: Let's create an asp.net application to understand
  •  In the design page(.aspx) place a DropDownList and a CheckBoxList control as:
      <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>


  • First we need to create sql server database e.g. "MyDataBase" and in that create two tables as:

Create a table Qualification_Tb as:
Dropdownlist and checkboxlist example on www.webcodeexpert.com

 Create a table Courses_Tb as:
Dropdownlist and checkboxlist example on www.webcodeexpert.com

  •  Create a connectionstring in the web.config file under configuration tag as:
<configuration>
<connectionStrings>
                                <add name="MyDbCon" connectionString="Data Source=LALIT;Initial Catalog=MyDataBase;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["MyDbCon"].ConnectionString);
        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(ConfigurationManager.ConnectionStrings["MyDbCon"].ConnectionString);
            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();
        }       
    }

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("MyDbCon").ConnectionString)
        Dim cmd As New SqlCommand("Select * from Qualification_Tb", con)
        Dim adp As New SqlDataAdapter(cmd)
        Dim dt As 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"));      
    End Sub

    Protected Sub ddlQual_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlQual.SelectedIndexChanged
        Dim dt As New DataTable()
        If ddlQual.SelectedIndex <> 0 Then
            Dim cmd As 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)
            Dim adp As 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 = Nothing
            cblCourses.DataBind()
        End If
    End Sub

Now over to you:

"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon