C# - 转发器中的下拉不工作

时间:2017-11-07 08:04:53

标签: c# asp.net .net asp.net-mvc-4 visual-studio-2012

protected void LoadRecords()
{
    List<PO.viewSO_Result> viewpolist = objDb.viewSO().ToList();

    CustomerRepeater.DataSource = viewpolist;
    CustomerRepeater.DataBind(); 
}

那是.aspx代码:

<asp:Repeater ID="CustomerRepeater" runat="server" >
    <ItemTemplate>
        <tr class="odd gradeX" > 
            <td><%# Eval("Supplier_order_id_pk") %></td> 
            <td>
                <asp:DropDownList runat="server" ID="dropdown1">
                    <asp:ListItem Text='<%# Eval("Supplier_order_id_pk") %>' Value='<%# Eval("Supplier_order_id_pk") %>'></asp:ListItem>
                </asp:DropDownList>
            </td> 
        </tr>
    </ItemTemplate>
</asp:Repeater>

我的DropDown不起作用或获取值。这是错误消息:

  

仅在具有DataBinding事件的对象上支持数据绑定表达式。 System.Web.UI.WebControls.ListItem没有DataBinding事件

我需要使用这种方式在转发器中创建DropDown。

1 个答案:

答案 0 :(得分:0)

试试这个

<asp:Repeater ID="CustomerRepeater" runat="server" OnItemDataBound="CustomerRepeater_ItemDataBound">
<ItemTemplate>
    <tr class="odd gradeX" > 
        <td> <asp:Label ID="lblOrderID" runat="server" Text='<%# Eval("Supplier_order_id_pk") %>'></asp:Label></td> 
        <td>
            <asp:DropDownList runat="server" ID="dropdown1">

            </asp:DropDownList>
        </td> 
    </tr>
</ItemTemplate>

protected void CustomerRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DropDownList ds = (DropDownList)e.Item.FindControl("dropdown1");
            ds.Items.Add((e.Item.FindControl("lblOrderID") as Label).Text);
        }
    }