如何绑定在转发器内的dropdownlist?

时间:2011-09-12 13:25:05

标签: 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:Repeater>

5 个答案:

答案 0 :(得分:15)

在您的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();

}

答案 1 :(得分:5)

使用Repeater的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属性。

答案 2 :(得分:4)

我刚刚找到了一种以声明方式做到这一点的方法:

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

&#34;地址&#34;在Eval()中使用的是使用后面的代码绑定到转发器的类的成员。在我的情况下,用作MyList的DataSource是一个List,它包含将在下拉列表中显示的可能值。

答案 3 :(得分:0)

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

HTML

<asp:Repeater runat="server" ID="repRoute" **OnItemCreated**="PopulateCountries">
            <ItemTemplate>
                <asp:DropDownList runat="server" ID="cboCountries" DataTextField="Name" DataValueField="CountryCode"/>&nbsp;
            </ItemTemplate>
        </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();
    }

答案 4 :(得分:0)

使用此

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

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

相关问题