Here I have created sample for you that populates repeater containing Cascading dropdownlist Country and state.
I hope this will help you out.
HTML
<asp:Repeater ID="rptAddress" runat="server" OnItemDataBound="Address_OnItemDataBound">
<HeaderTemplate>
<table>
<tr>
<th>
Name
</th>
<th>
Countries
</th>
<th>
States
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label Text='<%#Eval("Name") %>' runat="server" />
</td>
<td style="width: 40%; text-align: left">
<asp:DropDownList ID="ddlCountries" runat="server" Width="150px" AutoPostBack="true"
OnSelectedIndexChanged="Countries_OnSelectedIndexChanged">
</asp:DropDownList>
</td>
<td style="width: 40%; text-align: left">
<asp:DropDownList ID="ddlStates" runat="server" Width="150px" Enabled="false">
</asp:DropDownList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Code
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable names = new DataTable();
names.Columns.AddRange(new DataColumn[1] {
new DataColumn("Name", typeof(string))
});
names.Rows.Add("David");
names.Rows.Add("Muddasar");
this.rptAddress.DataSource = names;
this.rptAddress.DataBind();
}
}
protected void Address_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddlCountries = (DropDownList)e.Item.FindControl("ddlCountries");
ddlCountries.DataSource = this.GetCountries();
ddlCountries.DataTextField = "CountryName";
ddlCountries.DataValueField = "CountryId";
ddlCountries.DataBind();
ddlCountries.Items.Insert(0, new ListItem("", "0"));
}
}
protected void Countries_OnSelectedIndexChanged(object sender, EventArgs e)
{
RepeaterItem row = (RepeaterItem)(sender as DropDownList).NamingContainer;
DropDownList country = (DropDownList)row.FindControl("ddlCountries");
int countryId = int.Parse(country.SelectedItem.Value);
DropDownList ddlStates = (DropDownList)row.FindControl("ddlStates");
DataTable dtStates = this.GetStates(countryId);
if (dtStates.Rows.Count > 0)
{
ddlStates.DataSource = dtStates;
ddlStates.DataTextField = "StateName";
ddlStates.DataValueField = "StateId";
ddlStates.DataBind();
ddlStates.Enabled = true;
}
else
{
ddlStates.Enabled = false;
}
ddlStates.Items.Insert(0, new ListItem("", ""));
}
private DataTable GetCountries()
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CountryId,CountryName FROM Countries", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dtCountries = new DataTable();
sda.Fill(dtCountries);
return dtCountries;
}
}
}
}
private DataTable GetStates(int countryId)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT StateId,StateName FROM States WHERE CountryId = @CountryId", con))
{
cmd.Parameters.AddWithValue("@CountryId", countryId);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dtStates = new DataTable();
sda.Fill(dtStates);
return dtStates;
}
}
}
}
Screenshot
