Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding SelectedIndexChanged to generated checkboxlist

Tags:

c#

asp.net

I have a generated checkboxlist that is generated by selecting options on another checkboxlist:

CheckBoxList typefilter = new CheckBoxList { ID = "typefilter" + CheckBoxList1.Items[i].Text };
typefilter.RepeatDirection = RepeatDirection.Horizontal;
SortedList<int, string> filterValueList = new SortedList<int, string>();

typefilter.DataTextField = "Value";
typefilter.DataValueField = "Key";
typefilter.DataSource = getFilterOptions(int.Parse(CheckBoxList1.Items[i].Value));
typefilter.DataBind();

phSelectedItem.Controls.Add(typefilter);

Now the problem is if I want to give my generated checkboxlist a SelectedIndexChanged, how do I go about doing it?

Edit (After using the suggested handler method, clicking the checkbox will destroy the entire checkboxlist): Entire method's code

protected void cbl_RentalCategory_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cbl_RentalCategory.Items.Count > 0)
    {
        Label selectedType = new Label { ID = "Title" };
        selectedType.Text = "<h3>Rental Items</h3>";

        phSelectedItem.Controls.Add(selectedType);
    }

    for (int i = 0; i < cbl_RentalCategory.Items.Count; i++)
    {
        if (cbl_RentalCategory.Items[i].Selected)
        {
            Label selectedType = new Label { ID = "selectedType" + cbl_RentalCategory.Items[i].Text };
            selectedType.Text = cbl_RentalCategory.Items[i].Text + "<br/>";

            phSelectedItem.Controls.Add(selectedType);

            CheckBoxList typefilter = new CheckBoxList { ID = "typefilter" + cbl_RentalCategory.Items[i].Text };
            typefilter.RepeatDirection = RepeatDirection.Horizontal;
            SortedList<int, string> filterValueList = new SortedList<int, string>();
            typefilter.AutoPostBack = true;
            typefilter.SelectedIndexChanged += typefilter_SelectedIndexChanged;
            typefilter.DataTextField = "Value";
            typefilter.DataValueField = "Key";
            typefilter.DataSource = getFilterOptions(int.Parse(cbl_RentalCategory.Items[i].Value));
            typefilter.DataBind();

            phSelectedItem.Controls.Add(typefilter);

            Label nextLine = new Label { ID = "nextLine" + cbl_RentalCategory.Items[i].Text };
            nextLine.Text = "<br/>";
            phSelectedItem.Controls.Add(nextLine);
        }
    }
}

getFilterOptions():

private SortedList<int, string> getFilterOptions(int typeID)
{
    SortedList<int, string> filterValueList = new SortedList<int, string>();

    string strConnectionString = ConfigurationManager.ConnectionStrings["SOMEDB"].ConnectionString;
    SqlConnection conn = new SqlConnection(strConnectionString);

    using (SqlCommand cmd = new SqlCommand("Select * FROM SOMEWHERE WHERE ID=@ID", conn))
    {
        conn.Open();
        cmd.Parameters.AddWithValue("@ID", typeID);
        cmd.ExecuteNonQuery();
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            filterValueList.Add(int.Parse(reader["ID"].ToString()), reader["InformationHeader"].ToString());
        }

        conn.Close();
    }

    return filterValueList;
}
like image 743
Bloopie Bloops Avatar asked Jul 05 '26 22:07

Bloopie Bloops


1 Answers

Here is how you can do this.Set Autopostback to true and create selectedindexchanged method. Make sure you run this code only first time when page loads.

if(!IsPostBack){
CheckBoxList typefilter = new CheckBoxList { ID = "typefilter" + CheckBoxList1.Items[i].Text };
typefilter.RepeatDirection = RepeatDirection.Horizontal;
SortedList<int, string> filterValueList = new SortedList<int, string>();
typefilter.AutoPostBack = true;
typefilter.SelectedIndexChanged += typefilter_SelectedIndexChanged;
typefilter.DataTextField = "Value";
typefilter.DataValueField = "Key";     
typefilter.DataSource = getFilterOptions(int.Parse(CheckBoxList1.Items[i].Value));
typefilter.DataBind();

phSelectedItem.Controls.Add(typefilter);
}

and here is you selectedindex change method

public void typefilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        //your code
    }
like image 181
Mairaj Ahmad Avatar answered Jul 07 '26 13:07

Mairaj Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!