0

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.

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

Comments

0

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
    {
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.