【发布时间】:2015-04-18 14:32:07
【问题描述】:
我意识到这个问题之前可能已经被问过......但是我对此有一个特定的问题......
我的代码
foreach(ListViewItem item in listProducts.Items)
{
DropDownList dropList = listProducts.Items.FindControl("DropDownList1");
int SelectedID = Convert.ToInt32(listProducts.SelectedValue);
}
// now do something with that id...
问题是,一旦 foreach 循环通过 listview 项目,我就会从下拉列表中丢失所选项目的 ID...
我在 selectedindexchanged 下拉事件中使用此代码来定位所需的 ID,以便我可以根据用户从下拉列表中选择的内容向用户显示数据...
所以想法是当用户从下拉列表中选择某些东西时,我需要拿起所选项目的ID并在他选择时自动显示产品价格。
有人可以帮我解决这个问题吗?
列表视图:
<asp:ListView ID="listProducts" runat="server" DataKeyNames="ProductID" OnItemDataBound="listProducts_ItemDataBound" OnItemCommand="listProducts_ItemCommand" OnSelectedIndexChanging="listProducts_SelectedIndexChanging">
<ItemTemplate>
<div class="productoverlay">
<div class="col-lg-4 proizvod">
<div class="product">
<div class="glow"></div>
<img src='<%# "../productimg/" + Eval("FileName")%>' alt='<%# Eval("ProductName") %>'/>
</div>
</div>
<div class="col-lg-1 price">
<asp:Label ID="lblPrice" runat="server" Text=""></asp:Label>
</div>
<div class="col-lg-7 pushtop">
<h1><%# Eval("ProductName") %></h1>
<p>Description: </p>
<p><%# Eval("ProductDescription") %></p>
<p>Quantity: </p><asp:TextBox ID="txtPackageQuantity" TextMode="Number" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server" OnTextChanged="DropDownList1_TextChanged"></asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Add to cart" CommandName="AddToCart" CommandArgument='<%# Eval("ProductID")%>'/>
</div>
</div>
</ItemTemplate>
</asp:ListView>
【问题讨论】:
-
这段代码是在 DropDownList1_TextChanged 事件处理程序中定义的吗?
标签: c# asp.net listview drop-down-menu selectedindexchanged