Hi mkhader,
As per your query i have create the sample. If you have any doubt Please revert me back. Below is the sample.
HTML
<asp:Repeater ID="repCustomer" runat="server" OnItemDataBound="repCustomer_ItemDataBound">
<ItemTemplate>
<table>
<tr>
<td>
Country:<asp:DropDownList ID="ddlCountry" runat="server" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
AutoPostBack="true" AppendDataBoundItems="true">
<asp:ListItem Text="Select" Value="0" />
</asp:DropDownList>
</td>
<td>
State:
<asp:DropDownList ID="ddlStates" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="Select" Value="0" />
</asp:DropDownList>
</td>
<td>
<asp:Button Text="Save" runat="server" ID="btnSave" OnClick="Save" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("Name", typeof(string));
dt1.Rows.Add("Salman");
repCustomer.DataSource = dt1;
repCustomer.DataBind();
}
}
protected void repCustomer_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Country", typeof(string));
dt.Rows.Add(1, "India");
dt.Rows.Add(2, "Pakistan");
dt.Rows.Add(3, "SriLanka");
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddlCountry = (DropDownList)e.Item.FindControl("ddlCountry");
ddlCountry.DataSource = dt;
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "ID";
ddlCountry.DataBind();
}
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
RepeaterItem row = (RepeaterItem)(sender as DropDownList).NamingContainer;
DropDownList ddlStates = (DropDownList)row.FindControl("ddlStates");
int ddlCountry = Convert.ToInt32(((DropDownList)row.FindControl("ddlCountry")).SelectedValue);
DataTable dtStates = new DataTable();
dtStates.Columns.Add("ID", typeof(int));
dtStates.Columns.Add("States", typeof(string));
dtStates.Columns.Add("CountryId", typeof(int));
dtStates.Rows.Add(1, "Bihar", 1);
dtStates.Rows.Add(2, "U.P", 1);
dtStates.Rows.Add(3, "Lahor", 2);
dtStates.Rows.Add(4, "Pesawar", 2);
dtStates.Rows.Add(5, "Colombo", 3);
DataRow[] rows = dtStates.Select("CountryId = " + ddlCountry);
DataTable dtFinal = dtStates.Clone();
foreach (DataRow dr in rows) { dtFinal.Rows.Add(dr.ItemArray); }
ddlStates.Items.Clear();
ddlStates.DataSource = dtFinal;
ddlStates.DataTextField = "States";
ddlStates.DataValueField = "ID";
ddlStates.DataBind();
}
protected void Save(object sender, EventArgs e)
{
Button btn = (sender as Button);
string country = (btn.NamingContainer.FindControl("ddlCountry") as DropDownList).SelectedItem.Text;
string state = (btn.NamingContainer.FindControl("ddlStates") as DropDownList).SelectedItem.Text;
Response.Write(string.Format("Country : {0}<br/>State : {1}", country, state));
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt1 As New DataTable()
dt1.Columns.Add("Name", GetType(String))
dt1.Rows.Add("Salman")
repCustomer.DataSource = dt1
repCustomer.DataBind()
End If
End Sub
Protected Sub repCustomer_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
Dim dt As New DataTable()
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Country", GetType(String))
dt.Rows.Add(1, "India")
dt.Rows.Add(2, "Pakistan")
dt.Rows.Add(3, "SriLanka")
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim ddlCountry As DropDownList = DirectCast(e.Item.FindControl("ddlCountry"), DropDownList)
ddlCountry.DataSource = dt
ddlCountry.DataTextField = "Country"
ddlCountry.DataValueField = "ID"
ddlCountry.DataBind()
End If
End Sub
Protected Sub ddlCountry_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim row As RepeaterItem = DirectCast(TryCast(sender, DropDownList).NamingContainer, RepeaterItem)
Dim ddlStates As DropDownList = DirectCast(row.FindControl("ddlStates"), DropDownList)
Dim ddlCountry As Integer = Convert.ToInt32(DirectCast(row.FindControl("ddlCountry"), DropDownList).SelectedValue)
Dim dtStates As New DataTable()
dtStates.Columns.Add("ID", GetType(Integer))
dtStates.Columns.Add("States", GetType(String))
dtStates.Columns.Add("CountryId", GetType(Integer))
dtStates.Rows.Add(1, "Bihar", 1)
dtStates.Rows.Add(2, "U.P", 1)
dtStates.Rows.Add(3, "Lahor", 2)
dtStates.Rows.Add(4, "Pesawar", 2)
dtStates.Rows.Add(5, "Colombo", 3)
Dim rows As DataRow() = dtStates.Select("CountryId = " & ddlCountry)
Dim dtFinal As DataTable = dtStates.Clone()
For Each dr As DataRow In rows
dtFinal.Rows.Add(dr.ItemArray)
Next
ddlStates.Items.Clear()
ddlStates.DataSource = dtFinal
ddlStates.DataTextField = "States"
ddlStates.DataValueField = "ID"
ddlStates.DataBind()
End Sub
Protected Sub Save(sender As Object, e As EventArgs)
Dim btn As Button = TryCast(sender, Button)
Dim country As String = TryCast(btn.NamingContainer.FindControl("ddlCountry"), DropDownList).SelectedItem.Text
Dim state As String = TryCast(btn.NamingContainer.FindControl("ddlStates"), DropDownList).SelectedItem.Text
Response.Write(String.Format("Country : {0}<br/>State : {1}", country, state))
End Sub
Screenshot
