Hi bittu500,
Refer below sample.
HTML
C#
<table id="tbl1" class="table table-bordered table-scrollable table-condensed">
<thead>
<tr>
<th style="font-size: smaller;">SN</th>
<th style="font-size: smaller;">Country</th>
</tr>
</thead>
<tbody>
<asp:Repeater runat="server" ID="rptr1" OnItemDataBound="rptr1_OnItemDataBound">
<ItemTemplate>
<tr>
<td class="text-align-center"><%# (((RepeaterItem)Container).ItemIndex+1).ToString()%> </td>
<td class="text-align-left">
<asp:DropDownList ID="ddl1" runat="server"></asp:DropDownList>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
<div>
<asp:Button ID="btnSubmit" OnClick="btnSave_Click" runat="server" Text="Save & Submit" CausesValidation="false" />
</div>
VB.Net
<table id="tbl1" class="table table-bordered table-scrollable table-condensed">
<thead>
<tr>
<th style="font-size: smaller;">SN</th>
<th style="font-size: smaller;">Country</th>
</tr>
</thead>
<tbody>
<asp:Repeater runat="server" ID="rptr1" OnItemDataBound="rptr1_OnItemDataBound">
<ItemTemplate>
<tr>
<td class="text-align-center"><%#((CType(Container, RepeaterItem)).ItemIndex + 1).ToString()%> </td>
<td class="text-align-left">
<asp:DropDownList ID="ddl1" runat="server"></asp:DropDownList>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
<div>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSave_Click" Text="Save & Submit" CausesValidation="false" />
</div>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = GetData();
rptr1.DataSource = dt;
rptr1.DataBind();
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country")
});
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
return dt;
}
protected void rptr1_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataTable dtnew = GetData();
DropDownList ddl1 = (e.Item.FindControl("ddl1") as DropDownList);
ddl1.DataSource = dtnew;
ddl1.DataTextField = "Country";
ddl1.DataValueField = "Country";
ddl1.DataBind();
ddl1.Items.Insert(0, new ListItem { Text = "Select", Value = "" });
string country = (e.Item.DataItem as DataRowView)["Country"].ToString();
if (ddl1.Items.FindByValue(country) != null)
{
ddl1.Items.FindByValue(country).Selected = true;
}
}
}
protected void btnSave_Click(object sender, System.EventArgs e)
{
List<string> selected = new List<string>();
foreach (RepeaterItem item in rptr1.Items)
{
string selectedValue = (item.FindControl("ddl1") as DropDownList).SelectedValue;
if (!string.IsNullOrEmpty(selectedValue))
{
selected.Add(selectedValue);
}
}
if (selected.Count == 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select atleast an option.');", true);
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = GetData()
rptr1.DataSource = dt
rptr1.DataBind()
End If
End Sub
Private Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Return dt
End Function
Protected Sub rptr1_OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim dtnew As DataTable = GetData()
Dim ddl1 As DropDownList = (TryCast(e.Item.FindControl("ddl1"), DropDownList))
ddl1.DataSource = dtnew
ddl1.DataTextField = "Country"
ddl1.DataValueField = "Country"
ddl1.DataBind()
ddl1.Items.Insert(0, New ListItem With {.Text = "Select", .Value = ""})
Dim country As String = (TryCast(e.Item.DataItem, DataRowView))("Country").ToString()
If ddl1.Items.FindByValue(country) IsNot Nothing Then
ddl1.Items.FindByValue(country).Selected = True
End If
End If
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim selected As List(Of String) = New List(Of String)()
For Each item As RepeaterItem In rptr1.Items
Dim selectedValue As String = (TryCast(item.FindControl("ddl1"), DropDownList)).SelectedValue
If Not String.IsNullOrEmpty(selectedValue) Then
selected.Add(selectedValue)
End If
Next
If selected.Count = 0 Then
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Please select atleast an option.');", True)
End If
End Sub
Screenshot