Bibu
Bibu

Reputation: 201

checkboxes in asp.net using c#

Hy all..I want to ask you how is it possible when a checkbox is checked data from a table is shown based on that checked checkbox.So I have 2 tables: Countries and Cities. I displayed all the countries within a checkboxlist so each country has a checkbox.Now I want when I check a checkbox the cities regarding the country checked to appear.This is my code for displaying the countries:

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["erp"].ConnectionString);
    con.Open();
    string intero = "Select * from judete";
    SqlCommand cmd = new SqlCommand(intero, con);

    SqlDataReader rdr;

    rdr = cmd.ExecuteReader();

    while (rdr.Read())
    {


        CheckBoxList check = new CheckBoxList();
        check.Visible = true;

        check.Items.Add(new ListItem(rdr[1].ToString()));
        Panel1.Controls.Add(check);

        foreach (ListItem item in check.Items)
        {

            item.Text = rdr.GetString(1);
        }

    }

My question is: how can i retrieve the cities based on what checkbox is checked? Thanks in advance and sorry for repeating but I didn't figure it out yet.

Upvotes: 0

Views: 1366

Answers (2)

Ajay
Ajay

Reputation: 154

Use this code to get selected country

protected void CBCountries_SelectedIndexChanged(object sender, EventArgs e)
{
    string result = Request.Form["__EVENTTARGET"];
    string[] checkedBox = result.Split('$');
    int index = int.Parse(checkedBox[checkedBox.Length - 1]);

    if (CBCountries.Items[index].Selected)
    {
        String Country = CBCountries.Items[index].Value;
        //query your cities table based on selected Country
        BindCities(Country);     
    }
    else
    {
    }
}

Upvotes: 0

Alex
Alex

Reputation: 6149

what you should do is:

  1. make your checkboxlist autopostback property set to true
  2. on the event SelectedIndexChanged of checkboxlist write your code to retrieve the cities of the checkeditem and display them whereveer you want.

Upvotes: 2

Related Questions