1
votes

Controls below inside a repeater are placed in an update panel. First ddl has countries, its "selectedindexchanged" fills the second ddl which is for cities. Once you fill the textboxes and select the country and the city, and click the Add button, all controls' values kept in the repeater.

enter image description here

When you add a few more, always the final one's ddlCountry fires the ddl_SelectedIndexChanged(). If you try to change the previous one's ddlCountry value, ddl_SelectedIndexChanged() in the .cs file is not executed. I checked the page source: final ddlCountry's Id is

cphContent_ddlAddressCountry

and the previous one's Id is assigned sth like:

cphContent_rpAddress_lblCountrym_0

cphContent refers to UpdatePanel and rp does to Repeater.

I don't know how to catch the ddl's Id inside the repeater.

1

1 Answers

0
votes

Catching a control inside a repeater is possible in a few ways:

  1. Using the FindControl() method inside the ItemDatabound event (or one of the other events) of the repeater.
  2. Inside the SelectedIndexChanged() event. The sender parameter can be case as a DropDownList.

Code example of getting the a city ddl from inside a SelectedIndexChanged() event:

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ddlCountry = sender as DropDownList;
        var ddlCity = ddlCountry.Parent.FindControl("ddlCity") as DropDownList;
        ddlCity.DataSource = GetCities(ddlCountry.SelectedValue);
        ddlCity.DataBind();
    }

You should keep in mind that databinding the repeater with a different dataset could mess up the ID's of the drop down lists, and in turn mess up the firing of the SelectedIndexChanged event, because this event relies on comparing posted values to viewstate/controlstate values wich both are attached to the control id.