我正在为购物车设置构建目录页面,在该页面上,我使用中继器显示每个产品及其数据。我想使用下拉列表显示从 0 到手头的产品数量,这将使每个 DDL 对于页面上与之关联的产品是唯一的。
我似乎无法找到一种方法来绑定DDL,只看到了如何在中继器的ItemDataBound方法中绑定所有这些。
编辑:
我的网络表单
<asp:Repeater ID="rptProducts" runat="server" OnItemDataBound="rptProducts_ItemDataBound">
<ItemTemplate>
<div class="col-md-8 col-md-offset-2 product">
<img src="<%# Eval("ImageFile") %>" class="col-xs-12" alt="<%# Eval("Name") %> Product Image" />
<h3><%# Eval("Name") %></h3>
<p><%# Eval("ShortDescription") %></p>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</div>
</ItemTemplate>
</asp:Repeater>
My ItemDataBound(从另一个线程找到,这里(
protected void rptProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList1");
foreach(var prod in ProductsList)
{
ddl.Items.Add(new ListItem(prod.QuantOnHand.ToString(), prod.QuantOnHand.ToString()));
}
}
我显然没有正确使用 ItemDataBound(实际上从未使用过它(,因为这种绑定只是将每个数量添加到每个下拉列表中。
这是在 ItemDataBound 方法中填充 DropDownList 的最简单方法。在此代码段中,productQuantity
来自中继器源数据。但是您也可以执行数据库调用以获取正确的数量。然后你需要一个ID可以像这样得到:productQuantity = Convert.ToInt32(item["ID"]);
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList1");
//the product quantity variable
int productQuantity = 0;
//cast the repeater item back to a datarowview
DataRowView item = e.Item.DataItem as DataRowView;
//or if a List<objects> is bound, cast it back to it's class
//MyClass item = e.Item.DataItem as MyClass
//get the quantity from the repeater source item to fill the ddl
productQuantity = Convert.ToInt32(item["quantity"]);
//add the items to the ddl
for (int i = 0; i < productQuantity; i++)
{
ddl.Items.Add(new ListItem() { Text = i.ToString(), Value = i.ToString() });
}
//add a listitem to the ddl at position 0
ddl.Items.Insert(0, new ListItem() { Text = "Select Quantity", Value = "" });
}
对我来说,这听起来像是自定义控件的工作,其中您有一个名为 Quantity
的int
属性,并在 Render()
方法中使用它来写出 html 选项元素。
这是一个简化版本。您的最终代码将希望(至少(处理name
和ID
值:
[ToolboxData("<{0}:QuantityDropDown runat="server"></{0}:QuantityDropDown >")]
public class QuantityDropDown : Control
{
public int Quantity {
get { return (int)(ViewState["Quantity"] ?? default(int)); }
set {ViewState["Quantity"] = value;}
}
protected override void Render (HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.Select);
for (int i=0; i < Quantity; i++)
{
writer.AddAttribute("value", i);
writer.RenderBeginTag(HtmlTextWriterTag.Option);
writer.Write(i.ToString());
writer.RenderEndTag();
}
writer.RenderEndTag();
writer.WriteLine();
}
}
然后,可以将此控件的实例作为中继器标记的一部分包含在内。
<asp:Repeater ID="Repeater1" runat="server" ... >
<ItemTemplate>
<div class="col-md-8 col-md-offset-2 product">
<img src="<%# Eval("ImageFile") %>" class="col-xs-12" alt="<%# Eval("Name") %> Product Image"/>
<h3><%# Eval("Name") %></h3>
<p><%# Eval("ShortDescription") %></p>
<cc:QuantityDropDown ID="ItemQuantity" runat="server" Quantity='<%# Eval("Quantity")' />
</div>
</ItemTemplate>
</asp:Repeater>