简体   繁体   English

动态下拉列表

[英]Dynamic DropdownList

I have 2 questions: First, I put a DDL in a repeater in my .aspx.我有两个问题:首先,我将 DDL 放在我的 .aspx 中的中继器中。 Here's my code:这是我的代码:

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

well, the first question, is How can I populate my DDL from the code behind?好吧,第一个问题是如何从后面的代码填充我的 DDL? Then How can I do, to populate the DDL with <1-2-3-4> If the source value is 4. Thank you!!那我该怎么办,用 <1-2-3-4> 填充 DDL 如果源值为 4。谢谢!!

You can use the Init event to populate a DropDownList : 您可以使用Init事件填充DropDownList

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

To start u can use following 要开始你可以使用以下

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

At repeater's databinding event 在转发器的数据绑定事件

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

you need to use the repeater ItemDataBound event. 您需要使用转发器ItemDataBound事件。 how to bind dropdown??? 如何绑定下拉列表???

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

        }

in case of different logic apply on dropdown. 如果不同的逻辑适用于下拉列表。

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

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM