Bubbled event in dropdownlist

Posted by Sathya4260 under ASP.NET on 1/5/2011 | Points: 10 | Views : 1917 | Status : [Member] | Replies : 7
hi everyone,

When i selected the country from dropdownlist which is listed from server, the list of cities of that particular country should be shown in the dropdownlist which is also to be fetched from server, kindly help me on this...

Sathish Kumar S


Responses

Posted by: Karthikanbarasan on: 1/7/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
The below code will get the data from db and bind to database... the same way use it for rebinding...

try


{

con = new SqlConnection("Data Source=MCN0100;Initial Catalog = DropDowndataBase; Uid=sa; Pwd=");

cmd.CommandText = "select * from Record";

cmd.Connection = con;

con.Open();

DroplistData.DataSource = cmd.ExecuteReader();

DroplistData.DataTextField = "Name";

DroplistData.DataBind();

}

catch (Exception ex)

{

Response.Write(ex.StackTrace);

}


Thanks
Karthik
www.f5Debug.net

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suresh.mekkattil on: 1/9/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

First you need to catch the SelectedIndexChanged event of the dropdown, this is done simply double clicking the dropdown in the design view.

Then there you need to write the code to get the list of cities from the database table you have by pasing the country name. This is posible by writing the ado.net code in your SelectedIndexChanged event of the country dropdownlist.

if the list if obtained, simply bind the data to the dropdownlist for cities

-----
Suresh M

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sathya4260 on: 1/9/2011 [Member] Starter | Points: 25

Up
0
Down
hi dude,

suresh, Let me know how to achieve this by simply clicking once on the country download,

Karthick, Till now i fallowed to retrieve the data from DB only, just i want to know when selecting the country dropdown list the state dropdown list should binded dynamically, can you help me to solve this as soon as possible...



Sathish Kumar S

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/9/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi Sathya,

Here you go... the complete code to bind the details are as follows


Call the BindCountry in page load event of your page it will bind the country details to the country drop down list ddlCountry.


#region BindCountry

private void BindCountry()
{
SqlConnection Con = new SqlConnection();
string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlCommand SqlCmd = new SqlCommand();
string Str="select * from dbo.Country";
SqlCmd.CommandText = Str;
Con.ConnectionString = ConnectionString;

DataSet DS = new DataSet();
SqlDataAdapter SqlAdp = new SqlDataAdapter(Str,Con);
SqlAdp.Fill(DS,"Country");

if (DS.Tables[0].Rows.Count > 0)
{
foreach (DataRow Dr in DS.Tables[0].Rows)
{
ddlCountry.Items.Add(new ListItem(Dr["countryName"].ToString(), Dr["countryID"].ToString()));
}
}
}
#endregion


In the Selected index chagned of the ddlcountry (You can get this event by double clicking the dropdown in the design view

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) {
string county = ddCounties.SelectedItem.Value;
BindState(county);
}


This code will do the binding for states

private void BindState(string strCountry)
{
SqlConnection Con = new SqlConnection();
string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlCommand SqlCmd = new SqlCommand();
string Str="select * from dbo.states where country = " + strCountry; --Check this statement alone since i worked offline not sure abt compilation
SqlCmd.CommandText = Str;
Con.ConnectionString = ConnectionString;

DataSet DS = new DataSet();
SqlDataAdapter SqlAdp = new SqlDataAdapter(Str,Con);
SqlAdp.Fill(DS,"State");

if (DS.Tables[0].Rows.Count > 0)
{
foreach (DataRow Dr in DS.Tables[0].Rows)
{
ddlState.Items.Add(new ListItem(Dr["Statename"].ToString(), Dr["StateID"].ToString()));
}
}
}
#endregion



Kindly mark this answer if your issue is solved.

Thanks
Karthik
www.f5Debug.net

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sathya4260 on: 1/11/2011 [Member] Starter | Points: 25

Up
0
Down
thank karthik, but i need to make only one click on country dropdownlist, is it possible

Sathish Kumar S

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/11/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi Sathis,

The above code which i mentioned will have only one click rite? that wen we click the country drop down list everything happens.

Thanks
Karthik
www.f5Debug.net

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suresh.mekkattil on: 1/15/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi sathya,

When I say "double click the dropdown", I mean click within the Visual Studio, this is for getting the event(Normally Visual Studio will automatically get you the event when you double clicking the control) where you write your code (Karthic has provided a sample).

The dropdown will work in a single click, so you dont need to worry about that.

-----
Suresh M

Sathya4260, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response