2012-11-25 203 views
0

我有2個問題: 首先,我把DDL放在我的.aspx中的重複器中。 這裏是我的代碼:動態下拉列表

<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從後面的代碼? 那麼我該怎麼辦,用<填充DDL 1-2-3-4> 如果源數值是4. 謝謝!!

回答

1

可以使用Init事件來填充DropDownList

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

初始事件? 它在哪裏? – IsraGab

+0

您可以在'DropDownList'中找到它作爲'OnInit'事件。 –

0

啓動的u可以使用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())); 
       } 
      } 
     } 
0

你需要使用中繼器ItemDataBound事件。 如何綁定下拉菜單???

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

     }