无法获取asp listView所选项目索引

时间:2016-02-27 13:42:25

标签: c# listview

我正在尝试在listView标签上设置值。但listview适用于所有标签。

<asp:ListView ID="productListView" runat="server">
<ItemTemplate>
<div class="item col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="selection">
<asp:Label runat="server" CssClass="lblShowTag">BRAND:</asp:Label>
<asp:DropDownList ID="ddlBrand" runat="server" CssClass="branddrop" AutoPostBack="True" OnSelectedIndexChanged="ddlBrand_SelectedIndexChanged" ViewStateMode="Enabled" EnableViewState="true"> </asp:DropDownList>
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Label runat="server" ID="lblShowTag" text="PRICE: " CssClass="lblShowTag"/><asp:Label runat="server" ID="lblEachPrice" CssClass="lblEachPrice"/>
<asp:Label runat="server" ID="lblTotalPrice" CssClass="lblEachPrice" ForeColor="Green" Text="5000"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlBrand" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</div>
</ItemTemplate>
<EmptyDataTemplate>
<h3>Sorry...No Product Availabel</h3>
</EmptyDataTemplate>
</asp:ListView>

C#代码:

第一种方法。它只对0索引应用值。

 public void ddlBrand_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;


        Label lbl = productListView.Items[index:0].FindControl("lblEachPrice") as Label;
        lbl.Text = ddl.SelectedValue.ToString();

    }

第二种方法:在所有项目上应用所选值:

public void ddlBrand_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;

    foreach (ListViewItem item in productListView.Items)
    {
        Label lbl = item.FindControl("lblEachPrice") as Label;
        lbl.Text = ddl.SelectedValue.ToString();
    }
}

我想在Selected ListView或Click ListView Not All上设置Selected DropDownVaue。

1 个答案:

答案 0 :(得分:0)

也许这就是你想要的:

public void ddlBrand_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    ListViewItem item = (ListViewItem)(ddl.NamingContainer);
    Label lbl = item.FindControl("lblEachPrice") as Label;
    lbl.Text = ddl.SelectedValue.ToString();
}