我意识到之前可能已经问过这个问题......但我对此有一个具体问题......
我的代码
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>
答案 0 :(得分:1)
这可能适合你
protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
var lw = sender as ListView;
var ddl = lw.Items[e.NewSelectedIndex].FindControl("DropDownList1") as DropDownList;
var val = ddl.SelectedValue;
}
更新: dropwdownlists上没有OnTextChanged =“DropDownList1_TextChanged”(如聊天中所述) 如果你没有附加到listview事件但是附加到dropwdonwlist事件,你必须这样做:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = sender as DropDownList;
var val = ddl.SelectedValue;
}
答案 1 :(得分:0)
好的这是listview:
<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>
这是你需要的吗?