14

I want to bind dropdownlist which is inside a repeater.my code is

 <asp:Repeater ID="rep_UnAssignComps" runat="server">
    <ItemTemplate><asp:DropDownList ID="drp_CompPropAddress" runat="server">
            </asp:DropDownList></itemTemplate></asp:Repeater>
Ram Singh
  • 6,664
  • 35
  • 100
  • 166

5 Answers5

16

On your Repeater's ItemDatabound event use the following:

if (e.Item.ItemType == ListItemType.Item || 
         e.Item.ItemType == ListItemType.AlternatingItem)
{

    ((DropDownList)e.Item.FindControl("drp_CompPropAddress")).DataSource =(DataRowView) e.Item.DataItem;//Or any other datasource.
    ((DropDownList)e.Item.FindControl("drp_CompPropAddress")).DataBind();

}
Shell
  • 6,818
  • 11
  • 39
  • 70
6

Use the ItemDataBound event of the Repeater, like this:

protected void rep_UnAssignComps_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DropDownList selectList = e.Item.FindControl("drp_CompPropAddress") as DropDownList;
    if (selectList != null)
    {
        selectList.DataSource = SomeDataSource(); //your datasource
        selectList.DataBind();

        //selectList.DataTextField = "SomeColumn";
        //selectList.DataValueField = "SomeID";
    }
}

Also remember to set the DataTextField and DataValueField properties, either in the markup or in the ItemDataBound event.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
5

I just found a way to do this declaratively:

<asp:Repeater ID="rep_UnAssignComps" runat="server">
<ItemTemplate><asp:DropDownList ID="drp_CompPropAddress" runat="server" DataSource='<%# MyList %>' SelectedValue='<%# Eval("Address") %>'>
        </asp:DropDownList></itemTemplate></asp:Repeater>

The "Address" used in the Eval() is a member of the class that is bound to the repeater done using the code behind. The DataSource used as MyList is a List in my case, that contains the possible values that will show up in the dropdown.

Yatin
  • 1,178
  • 11
  • 15
0

Use Repeater's OnItemCreated event and bind the dropdowns inside it.

HTML

<asp:Repeater runat="server" ID="repRoute" **OnItemCreated**="PopulateCountries">
            <ItemTemplate>
                <asp:DropDownList runat="server" ID="cboCountries" DataTextField="Name" DataValueField="CountryCode"/>&nbsp;
            </ItemTemplate>
        </asp:Repeater>

Codebehind:

protected void PopulateLocations(object sender, RepeaterItemEventArgs e)
    {
        var customerInfo = (CustomerInfo)e.Item.DataItem;
        if (customerInfo == null) return;

        var cboCountries = (DropDownList)e.Item.FindControl("cboCountries");
        cboCountries.DataSource = GetAll();
        cboCountries.DataBind();
    }
Subrata Sarkar
  • 2,975
  • 6
  • 45
  • 85
0

use this

<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>
        </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>
    </tr>
</ItemTemplate>
<FooterTemplate>
    </table>
</FooterTemplate>
</asp:Repeater>

and in codebehind:

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;
}
}

from https://www.aspsnippets.com/Articles/Populate-Bind-DropDownList-in-ItemTemplate-of-Repeater-Control-in-ASPNet.aspx