ASP.NET - DROPDOWNLIST IN C#

Asked By vinayak on 22-Dec-11 02:51 AM
HI FRIENDS,

I HAVE A TWO DROPDOWNLIST. FIRST DROPDOWNLIST FOR STATES,SECOND DROPDOWNLIST  FOR CAPITAL.

I WANT TO SHOW LIKE THIS.

IF SELECTED STATE DROPDOWNLIST , AUTOMATICALLY SHOWS  THE REVELANT CAPITAL IN SECOND DROPDOWNLIST  WITH USING DATABASE.
dipa ahuja replied to vinayak on 22-Dec-11 02:57 AM
First of all set the AutoPostBack=True for all the dropdownlist. then implement the selectedIndexChanged This way  
 
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
  string s = DropDownList1.SelectedValue.ToString();
  SqlConnection conn = new SqlConnection();
  conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlDataAdapter da = new SqlDataAdapter("select State from table1 where Country='" + s + "'", conn);
  DataSet ds = new DataSet();
 
  da.Fill(ds); // Fill the dataset
  DropDownList2.DataSource = ds;
  DropDownList2.DataTextField = "State";
  DropDownList2.DataValueField = "State";
 
  DropDownList2.DataBind();
}
//Bind City
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
  string s = DropDownList2.SelectedValue.ToString();
  SqlConnection conn = new SqlConnection();
  conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlDataAdapter da = new SqlDataAdapter("select City from table1 where State='" + s + "'", conn);
  DataSet ds = new DataSet();
 
  da.Fill(ds); // Fill the dataset
  DropDownList3.DataSource = ds;
  DropDownList3.DataTextField = "City";
  DropDownList3.DataValueField = "City";
 
  DropDownList3.DataBind();
}
 
 
 
kalpana aparnathi replied to vinayak on 22-Dec-11 03:10 AM
HI,
using System;
  using System.Web;
  using System.Collections;
  using System.Collections.Generic;
  using System.Collections.Specialized;
  using System.Web.Services;
  using System.Web.Services.Protocols;
  using AjaxControlToolkit;
  using System.Data;
  using System.Data.SqlClient;
 
  /// <summary>
  /// Summary description for CarData
  /// </summary>
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  public class CarData : System.Web.Services.WebService
  {
    public CarData()
    {
    //Uncomment the following line if using designed components
    //InitializeComponent();
    }
 
    [WebMethod]
    public CascadingDropDownNameValue[] GetMakes(
    string knownCategoryValues,
    string category)
    {
    CarsTableAdapters.MakeTableAdapter makeAdapter =
      new CarsTableAdapters.MakeTableAdapter();
    Cars.MakeDataTable makes = makeAdapter.GetMakes();
    List<CascadingDropDownNameValue> values =
      new List<CascadingDropDownNameValue>();
    foreach (DataRow dr in makes)
    {
      string make = (string)dr["Make"];
      int makeId = (int)dr["MakeID"];
      values.Add(new CascadingDropDownNameValue(
      make, makeId.ToString()));
    }
    return values.ToArray();
    }
 
    [WebMethod]
    public CascadingDropDownNameValue[] GetModelsForMake(
    string knownCategoryValues,
    string category)
    {
    StringDictionary kv =
    CascadingDropDown.ParseKnownCategoryValuesString(
      knownCategoryValues);
    int makeId;
    if (!kv.ContainsKey("Make") ||
      !Int32.TryParse(kv["Make"], out makeId))
    {
      return null;
    }
    CarsTableAdapters.ModelTableAdapter makeAdapter =
      new CarsTableAdapters.ModelTableAdapter();
    Cars.ModelDataTable models =
      makeAdapter.GetModelsForMake(makeId);
    List<CascadingDropDownNameValue> values =
      new List<CascadingDropDownNameValue>();
    foreach (DataRow dr in models)
    {
      values.Add(new CascadingDropDownNameValue(
      (string)dr["Model"], dr["ModelID"].ToString()));
    }
    return values.ToArray();
    }
 
    [WebMethod]
    public CascadingDropDownNameValue[] GetColorsForModel(
    string knownCategoryValues,
    string category)
    {
    StringDictionary kv =
      CascadingDropDown.ParseKnownCategoryValuesString(
      knownCategoryValues);
    int modelId;
    if (!kv.ContainsKey("Model") ||
      !Int32.TryParse(kv["Model"], out modelId))
    {
      return null;
    }
    CarsTableAdapters.ColorTableAdapter adapter =
      new CarsTableAdapters.ColorTableAdapter();
    Cars.ColorDataTable colorTable =
      adapter.GetColorsForModel(modelId);
    List<CascadingDropDownNameValue> values =
      new List<CascadingDropDownNameValue>();
    foreach (DataRow dr in colorTable)
    {
      values.Add(new CascadingDropDownNameValue(
      (string)dr["Color"], dr["ColorID"].ToString()));
    }
    return values.ToArray();
    }
Jitendra Faye replied to vinayak on 22-Dec-11 03:48 AM

If you want to display data based on DropDownList selection, means if you want to implement cascading then you have to implement code in SelectedIndexChanged() Event of DropDownList.

before that set AutoPostBack Property of All DropDownList ti true.

I m giving sample code for that-

Here i m doing cascading for country, state and city.

1. here i m filling DropDownList1 with county

//for binding DropDownList1 with country

private void DataGrid_Load(object sender, EventArgs e)
{
getdata();
}

//function for getting country
private void getdata()
{
SqlConnection cn = new SqlConnection("constring");
cn.Open();
string strQuery = "Select conid,conname from countrytab";
SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DisplayMember = "conid";
DropDownList1.ValueMember = "conname";
DropDownList1.DataSource = ds.Tables[0];
}

2. here i m filling DropDownList2 with state, based on selection on DropDownList1

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("constring");
cn.Open();
string strQuery = "Select stateid,statename from statetab where conid='" + DropDownList1.SelectedValue .ToString() + "'";
SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList2 .DisplayMember = "stateid";
DropDownList2.ValueMember = "statename";
DropDownList2.DataSource = ds.Tables[0];
}

3. here i m filling DropDownList3 with city, based on selection on DropDownList2

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("constring");
cn.Open();
string strQuery = "Select cityid,cityname from citytab where conid='" + DropDownList1.SelectedValue .ToString() + "' and stateid='" + DropDownList2.SelectedValue .ToString() + "' ";
SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList3.DisplayMember = "cityid";
DropDownList3.ValueMember = "cityname";
DropDownList3.DataSource = ds.Tables[0];
}

USE THIS CODE AND LET ME KNOW.

Anoop S replied to vinayak on 22-Dec-11 05:33 AM
1. Set AutoPostBack Property of DropDownList to True

2. Wrtite following code in SelectedIndexChanged Event(); 


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
SqlConnection cn = new SqlConnection("constring"); 
cn.Open();
string strQuery = "Select capital from tablename where state='" + DropDownList1.SelectedItem.ToString() + "'"; 
SqlDataAdapter da = new SqlDataAdapter(strQuery, cn); 
DataSet ds = new DataSet(); 
da.Fill(ds);
DropDownList2 .DisplayMember = "captal"; 
DropDownList2.ValueMember = "capital"; 
DropDownList2DataSource = ds.Tables[0];
}