In this article I will explain with an example, how to get selected Text and Value of DropDownList in ItemTemplate of Repeater Control on Button Click in ASP.Net using C# and VB.Net.
The DropDownList will be populated from database inside the OnItemDataBound event of the Repeater control in ASP.Net using C# and VB.Net.
Each Repeater Item has a Button and when the Button is clicked, the selected Text and Value of the DropDownList is fetched and displayed using JavaScript Alert Message Box.
 
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
 
HTML Markup
The following HTML Markup consists of an ASP.Net Repeater control with an HTML Table Layout.
Note: For more information on displaying HTML Table Layout using Repeater control, please refer Repeater control Tutorial with example in ASP.Net using C# and VB.Net.
 
The Repeater’s ItemTemplate consists of two Label controls, a DropDownList control and a Button.
<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="OnItemDataBound">
    <HeaderTemplate>
        <table cellspacing="0" rules="all" border="1">
            <tr>
                <th scope="col" style="width: 80px">
                    Customer Id
                </th>
                <th scope="col" style="width: 120px">
                    Name
                </th>
                <th scope="col" style="width: 100px">
                    Country
                </th>
                <th></th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' />
            </td>
            <td>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("ContactName") %>' />
            </td>
            <td>
                <asp:DropDownList ID="ddlCountries" runat="server">
                </asp:DropDownList>
            </td>
            <td>
                <asp:Button ID = "btnGet" Text="Get Value" runat="server" OnClick = "GetValue" />
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
 
 
Populating Repeater control from database in ASP.Net
Inside the Page load event handler, the Repeater is populated with the records from the Customers table.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        rptCustomers.DataSource = this.GetData("SELECT TOP 10 CustomerId, ContactName, Country FROM Customers");
        rptCustomers.DataBind();
    }
}
 
private DataTable GetData(string query)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        rptCustomers.DataSource = Me.GetData("SELECT TOP 10 CustomerId, ContactName, Country FROM Customers")
        rptCustomers.DataBind()
    End If
End Sub
 
Private Function GetData(ByVal query As String) As DataTable
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand(query, con)
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Dim dt As DataTable = New DataTable()
                sda.Fill(dt)
                Return dt
            End Using
        End Using
    End Using
End Function
 
 
Populate (Bind) DropDownList in ItemTemplate of Repeater Control in ASP.Net
Inside the OnItemDataBound event handler, first the DropDownList is referenced using the FindControl method and then it is populated with records of Countries from database.
The Country value of the respective Customer is fetched from the DataItem object of the Repeater and is used to select the Country inside the DropDownList control.
C#
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //Find the DropDownList in the Repeater Item.
        DropDownList ddlCountries = (e.Item.FindControl("ddlCountries") as DropDownList);
        ddlCountries.DataSource = this.GetData("SELECT DISTINCT Country FROM Customers");
        ddlCountries.DataTextField = "Country";
        ddlCountries.DataValueField = "Country";
        ddlCountries.DataBind();
 
        //Add Default Item in the DropDownList.
        ddlCountries.Items.Insert(0, new ListItem("Please select"));
 
        //Select the Country of Customer in DropDownList.
        string country = (e.Item.DataItem as DataRowView)["Country"].ToString();
        ddlCountries.Items.FindByValue(country).Selected = true;
    }
}
 
VB.Net
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        'Find the DropDownList in the Repeater Item.
        Dim ddlCountries As DropDownList = (TryCast(e.Item.FindControl("ddlCountries"), DropDownList))
        ddlCountries.DataSource = Me.GetData("SELECT DISTINCT Country FROM Customers")
        ddlCountries.DataTextField = "Country"
        ddlCountries.DataValueField = "Country"
        ddlCountries.DataBind()
 
        'Add Default Item in the DropDownList.
        ddlCountries.Items.Insert(0, New ListItem("Please select"))
 
        'Select the Country of Customer in DropDownList.
        Dim country As String = (TryCast(e.Item.DataItem, DataRowView))("Country").ToString()
        ddlCountries.Items.FindByValue(country).Selected = True
    End If
End Sub
 
 
Get Selected Text and Value of DropDownList in Repeater on Button click in ASP.Net
When the Button is clicked, first the Repeater Item is referenced using the NamingContainer property.
Then the DropDownList control is referenced from the Repeater Item using the FindControl method.
Finally the Selected Text and Value of the DropDownList control is displayed using JavaScript Alert Message Box.
C#
protected void GetValue(object sender, EventArgs e)
{
    //Reference the Repeater Item using Button.
    RepeaterItem item = (sender as Button).NamingContainer as RepeaterItem;
 
    //Reference the DropDownList.
    DropDownList ddlCountries = item.FindControl("ddlCountries") as DropDownList;
 
    //Get the Selected Text.
    string selectedText = ddlCountries.SelectedItem.Text;
 
    //Get the Selected Value.
    string selectedValue = ddlCountries.SelectedItem.Value;
 
    string message = string.Format("Selected Text: {0} Selected Value: {1}", selectedText, selectedValue);
 
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
 
VB.Net
Protected Sub GetValue(ByVal sender As Object, ByVal e As EventArgs)
    'Reference the Repeater Item using Button.
    Dim item As RepeaterItem = TryCast((TryCast(sender, Button)).NamingContainer, RepeaterItem)
 
    'Reference the DropDownList.
    Dim ddlCountries As DropDownList = TryCast(item.FindControl("ddlCountries"), DropDownList)
 
    'Get the Selected Text.
    Dim selectedText As String = ddlCountries.SelectedItem.Text
 
    'Get the Selected Value.
    Dim selectedValue As String = ddlCountries.SelectedItem.Value
 
    Dim message As String = String.Format("Selected Text: {0} Selected Value: {1}", selectedText, selectedValue)
 
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
 
 
Screenshot
Get Selected Value of DropDownList in Repeater Control in ASP.Net
 
 
Demo
 
 
Downloads