使用eval结果动态填充asp C#中的下拉列表

时间:2012-12-05 06:48:09

标签: c# asp.net

我正在创建一个购物车。我在listview中有一个下拉列表。我想简单地根据eval(“stock”)添加一系列数字,0到库存或10。

<% int range = atoi(eval("..")) // don't know the correct conversion syntax, not to that point yet..
if(range >10)
 range = 10
for(i = 0; i < range; i++){
 dropdown1.Item.append( new listitem...)
} 
%>

我尝试过使用函数调用,但我无法理解。我在脚本标签和cs文件中都尝试过它。

任何能帮助我指向正确方向的帮助都会很棒。我是新人,现在我已经研究了几个小时了。 2本50美元的书籍,他们在这些列表视图模板项目中没有任何帮助..

     <AlternatingItemTemplate>
            <span style="">
            <asp:Label ID="productNameLabel" runat="server" 
                Text='<%# Eval("productName") %>' />
            <br />
            <asp:Image runat="server" height = "300" ImageUrl='<%# Eval("img") %>'></asp:Image>
            <br />
            Description:<br />
            <asp:Label ID="itemNotesLabel" runat="server" Text='<%# Eval("itemNotes") %>' />
            <br />

            stock:
            <asp:Label ID="stockLabel" runat="server" Text='<%# Eval("stock") %>' />
            <br />
            price:
            <asp:Label ID="priceLabel" runat="server" Text='<%# "$"+ Eval("price")+".00" %>' />
            <br />

            Quantitiy:<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList> 

            <br />

            <asp:LinkButton id="addPro" runat="server" CommandArgument='<%# Eval("productNo") %>' onCommand ="addPro_Click">Add To Cart</asp:LinkButton>
            <br /><br /><br />
<br /></span>
        </AlternatingItemTemplate>

2 个答案:

答案 0 :(得分:1)

在我的示例中,我假设您有一个ProductId的数据键,ListView可以找到特定的Stock。我添加了OnItemDataBound方法,我在后面的代码中使用它来填充下拉列表。

<强>标记

<asp:ListView ID="lvStock" runat="server" OnItemDataBound="lvStock_ItemDataBound" .... >


代码背后

 protected void lvStock_ItemDataBound(object sender, ListViewItemEventArgs e)
 {

       if (e.Item.ItemType == ListViewItemType.DataItem) 
       {
           // Get the Product Id (or whatever ID it is)
           ListView listView = sender as ListView;
           int index = e.Item.DataItemIndex;
           DataKey dataKey = listView.DataKeys[index];
           int productId = Convert.ToInt32(dataKey["ProductId"]);

           // Get the stock value from your DB or wherever you get it from
           int stock = GetStockById(productId);


           if (stock > 10)
               stock = 10;

           // Get the stock drop down list
           DropDownList ddlListStock = (DropDownList )e.Item.FindControl("DropDownList1");

           // add the values to the drop down list
           for (int i = 0; i <= stock; i++)
           {
                ddlListStock.Items.Add(i.ToString());
           }
       }
  }

答案 1 :(得分:0)

您需要处理ItemDataBound ListView事件,以填充添加到每个项目中的DropDownList控件。

演示:

标记(.aspx)代码

<asp:ListView ID="ListView1" runat="server">
    <ItemTemplate>
    <p>
        <asp:Label ID="stock" runat="server" Text='<%#Eval("Stock") %>'></asp:Label>
        <asp:DropDownList ID="stockQty" runat="server"></asp:DropDownList>
    </p>
    </ItemTemplate>
</asp:ListView>

代码隐藏(.cs)

public class Foo
{
    public int Stock { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<Foo> fooList = new List<Foo>()
        {
                new Foo(){ Stock=3},
                new Foo(){ Stock=5}
        };
        ListView1.ItemDataBound += (sa, ea) =>
            {
                int stock = int.Parse((ea.Item.FindControl("Stock") as Label).Text);
                DropDownList stockQty = ea.Item.FindControl("StockQty") as DropDownList;

                for (int i = 0; i <= stock; i++)
                    stockQty.Items.Add(i.ToString());
            };

        ListView1.DataSource = fooList;
        ListView1.DataBind();
    }
}