Asp.net 如何绑定中继器内部的dropdownlist?

Asp.net 如何绑定中继器内部的dropdownlist?,asp.net,drop-down-menu,repeater,Asp.net,Drop Down Menu,Repeater,我想绑定dropdownlist,它位于中继器内。我的代码是 <asp:Repeater ID="rep_UnAssignComps" runat="server"> <ItemTemplate><asp:DropDownList ID="drp_CompPropAddress" runat="server"> </asp:DropDownList></itemTemplate></asp:Rep

我想绑定dropdownlist,它位于中继器内。我的代码是

 <asp:Repeater ID="rep_UnAssignComps" runat="server">
    <ItemTemplate><asp:DropDownList ID="drp_CompPropAddress" runat="server">
            </asp:DropDownList></itemTemplate></asp:Repeater>

在中继器的
ItemDatabound
事件上,使用以下命令:

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();

}

使用中继器的ItemDataBound事件,如下所示:

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

另外,请记住在标记或ItemDataBound事件中设置DataTextField和DataValueField属性。

我刚刚找到了一种以声明方式执行此操作的方法:

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


Eval()中使用的“地址”是使用代码隐藏绑定到中继器的类的成员。在我的例子中,用作MyList的数据源是一个列表,其中包含将在下拉列表中显示的可能值。

使用Repeater的
OnItemCreated
事件并将下拉列表绑定到其中

HTML 用这个

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

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