Saturday, July 28, 2012

Working with Drop Down Cascading Drop Down

I am using Three Drop Down here

Default.aspx



<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:DropDownList ID="ddlcounty" runat="server" AutoPostBack="true" 
            onselectedindexchanged="ddlcounty_SelectedIndexChanged">
        </asp:DropDownList>
  <asp:DropDownList ID="ddlstate" runat="server" 
            onselectedindexchanged="ddlstate_SelectedIndexChanged" AutoPostBack="true">
        </asp:DropDownList>
        <asp:DropDownList ID="ddlcity" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>


Default.aspx.cs



using System.Data;

using System.Data.SqlClient;

public partial class dropdown : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection("user id=sa;password=uday;database=country;data source=localhost");
            SqlDataAdapter da = new SqlDataAdapter("select * from tblcountry", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ddlcounty.DataTextField = "countryname";
            ddlcounty.DataValueField = "countryid";
            ddlcounty.DataSource = ds;
            ddlcounty.DataBind();
            ddlcounty.Items.Insert(0, new ListItem("select", "0"));
            ddlstate.Items.Insert(0, new ListItem("select", "0"));
            ddlcity.Items.Insert(0, new ListItem("select", "0"));
        }

    }

    protected void ddlcounty_SelectedIndexChanged(object sender, EventArgs e)
    {
        int countryid = Convert.ToInt16(ddlcounty.SelectedValue);
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=country;data source=localhost");
        SqlDataAdapter da = new SqlDataAdapter("select * from tblstate where countryid="+countryid, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlstate.DataTextField = "statename";
        ddlstate.DataValueField = "stateid";
        ddlstate.DataSource = ds;
        ddlstate.DataBind();
        ddlstate.Items.Insert(0, new ListItem("Select", "0"));
        if (ddlstate.SelectedValue == "0")
        {
            ddlcity.Items.Clear();
            ddlcity.Items.Insert(0, new ListItem("Select", "0"));
        }
    }


    protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)

    {
        int stateid = Convert.ToInt16(ddlstate.SelectedValue);
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=country;data source=localhost");
        SqlDataAdapter da = new SqlDataAdapter("select * from tblcity where stateid="+stateid, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlcity.DataTextField = "cityname";
        ddlcity.DataValueField = "cityid";
        ddlcity.DataSource = ds;
        ddlcity.DataBind();
        ddlcity.Items.Insert(0, new ListItem("Select", "0"));
    }
}



Database design

tblcountry



tblstate




tblcity



output






No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...