Friday 19 September 2014

Asphelp: Cascade Dropdownlist Using C#

Download Source

In this article you will learn how to make cascading dropdownlist.

Create Table Automobile

table automobile | dotnetasphelp


Create Table Brand

table brand | dotnetasphelp


Create Table Model

table model | dotnetasphelp


HTML CODE
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        .textbox {
            background: white;
            border: 1px double #DDD;
            border-radius: 5px;
            box-shadow: 0 0 5px #333;
            color: #666;
            outline: none;
            height: 25px;
            width: 230px;
        }
    </style>

</head>
<body style="background-color: #F5FAFA">
    <form id="form1" runat="server">
        <div align="center">
            <asp:ScriptManager runat="server"></asp:ScriptManager>
            <asp:UpdatePanel runat="server">
                <ContentTemplate>
                    <table>
                        <tr>
                            <td align="center" style="font-size: large">Cascading Dropdown list<br />
                                <br />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:DropDownList ID="ddlautomobile" runat="server" CssClass="textbox" AutoPostBack="true" OnSelectedIndexChanged="ddlautomobile_SelectedIndexChanged" Font-Size="Medium"></asp:DropDownList><br />
                                <br />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:DropDownList ID="ddlbrand" runat="server" CssClass="textbox" OnSelectedIndexChanged="ddlbrand_SelectedIndexChanged" AutoPostBack="true" Font-Size="Medium"></asp:DropDownList><br />
                                <br />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:DropDownList ID="ddlmodel" runat="server" CssClass="textbox" Font-Size="Medium"></asp:DropDownList></td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>


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.SqlClient;
using System.Data;
using System.Configuration;

namespace CascadingDropdownlist
{
    public partial class Home : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=Servername;Initial Catalog=Databasename;Integrated Security=True");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindautomobile();
                ddlbrand.Items.Insert(0, new ListItem("--------------   Select   --------------", "0"));
                ddlmodel.Items.Insert(0, new ListItem("--------------   Select   --------------", "0"));
            }
        }

        public void bindautomobile()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from automobile", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            ddlautomobile.DataSource = ds.Tables[0];
            ddlautomobile.DataValueField = "autoid";
            ddlautomobile.DataTextField = "autoname";
            ddlautomobile.DataBind();
            ddlautomobile.Items.Insert(0, new ListItem("--------------   Select   --------------", "0"));
        }     

        protected void ddlautomobile_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from brand where autoid='" + ddlautomobile.SelectedItem.Value + "'", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            ddlbrand.DataSource = ds.Tables[0];
            ddlbrand.DataValueField = "brandid";
            ddlbrand.DataTextField = "brandname";
            ddlbrand.DataBind();
            ddlbrand.Items.Insert(0, new ListItem("--------------   Select   --------------", "0"));
        }

        protected void ddlbrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from model where brandid='" + ddlbrand.SelectedItem.Value + "'", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            ddlmodel.DataSource = ds.Tables[0];
            ddlmodel.DataValueField = "modelid";
            ddlmodel.DataTextField = "modelname";
            ddlmodel.DataBind();
            ddlmodel.Items.Insert(0, new ListItem("--------------   Select   --------------", "0"));
        }
    }
}


Output Preview

 cascade dropdownlist | dotnetasphelp


Download Source

No comments: