动态DropdownList

时间:2012-11-25 16:06:03

标签: c# asp.net ddl

我有两个问题: 首先,我在我的.aspx中将DDL放在转发器中。 这是我的代码:

<HeaderTemplate>
    <ul class ="horizontal">
  </HeaderTemplate>
  <ItemTemplate>
    <li>
<img src="<%# DataBinder.Eval(Container.DataItem, "ImagePath") %>" width="60" height="40" alt = "<%# DataBinder.Eval(Container.DataItem, "ProductName") %>"/>
        <p>ID: <asp:Literal ID="ProductIDLiteral" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>'></asp:Literal></p>
        <p>
            <asp:Literal ID="NameLiteral" runat="server" Text="Name: "></asp:Literal><asp:Literal ID="ProductNameLiteral" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "ProductName") %>'></asp:Literal>
        </p>
        <p>
            <asp:Literal ID="Literal1" runat="server" Text="Price: "></asp:Literal><asp:Literal ID="UnitPriceLiteral" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "UnitPrice") %>'></asp:Literal>
         </p>
         <p> <asp:Literal ID="QuantityLiteralLiteral" runat="server" Text="Quantity: "></asp:Literal><asp:DropDownList ID="DDLQuantity" runat="server" ><asp:ListItem></asp:ListItem>
        </asp:DropDownList></p>
        <asp:Literal ID="Literal" runat="server"></asp:Literal>
 </li>
  </ItemTemplate>
  <FooterTemplate>
      <asp:Button ID="Button1" runat="server" Text="Validate!" />
    </ul>
  </FooterTemplate>
</asp:Repeater>

好吧,第一个问题是,如何从后面的代码中填充我的DDL? 然后我该怎么做,用&lt; 1-2-3-4&gt;填充DDL。 如果源值为4。 谢谢!!

3 个答案:

答案 0 :(得分:1)

您可以使用Init事件填充DropDownList

protected void DropDownList1_Init(object sender, EventArgs e)
{
    for (int i = 1; i <= 4; i++)
    {
        ((DropDownList)sender).Items.Add(i.ToString());
    }
}

答案 1 :(得分:0)

开始你可以使用folliwing

public int? Number //number of elements. It can be stored in viewstate etc. depends on logic
        {
            get
            {
                return (int?)ViewState["number"];
            }
            set
            {
                ViewState["number"] = value;
            }
        }

在转发器的数据绑定事件中

protected void Repeater1_DataBinding(object sender, RepeaterItemEventArgs  e)
        { 
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==                  
                 ListItemType.AlternatingItem)
            {
                DropDownList DDLQuantity = (DropDownList) e.Item.FindControl("DDLQuantity");
                if (DDLQuantity != null)
                { 
                    for (int i=0;i<Number;i++)
                        DDLQuantity.Items.Add(new ListItem(i.ToString(),i.ToString()));
                }
            }
        }

答案 2 :(得分:0)

您需要使用转发器ItemDataBound事件。 如何绑定dropdown ???

protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DropDownList dd = (DropDownList)e.Item.FindControl("control_id");
            dd.DataSource = "";
            dd.DataBind();

        }

如果在下拉列表中应用了不同的逻辑。

protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            /////////////
            //logic for source_value
            /////////////
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                yourObj obj = (yourObj)e.Item.DataItem;
                if (yourObj.source_value == 4)
                {
                    dd.Items.Add("one");
                    dd.Items.Add("two");
                    dd.Items.Add("three");
                    dd.Items.Add("four");
                }
            }

        }