当我点击listview中的按钮时,我有一个关于从asp.net中的listview获取对象(或至少ID对象)的问题。 SelectedIndexChanged事件不会触发。还有其他选择吗?
我的列表视图中有2个图像按钮(信息和购物车):
我的aspx代码:
<asp:ListView ID="ListView1" runat="server" OnSelectedIndexChanged="ListView1_SelectedIndexChanged">
<ItemTemplate>
<tr>
<td class="text-left"><%# Eval("naziv")%></td>
<td class="text-left"><%# Eval("cena")%></td>
<td class="text-left">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/info_512pxGREY.png" Height="50px" Width="50px" /></td>
<td class="text-left">
<asp:ImageButton ID="ImageButton2" runat="server" Height="50px" Width="50px" ImageUrl="https://cdn2.iconfinder.com/data/icons/picons-basic-2/57/basic2-011_shopping_cart-512.png" />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table class="table-fill" id="tbl1" runat="server">
<thead>
<tr id="tr1" runat="server">
<th id="td1" runat="server">Naziv</th>
<th id="td2" runat="server">Cena</th>
<th id="td3" runat="server">Info</th>
<th id="td4" runat="server">Kupi</th>
</tr>
</thead>
<tr id="ItemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
答案 0 :(得分:0)
该按钮正在截取该事件,请使用OnCommand
<asp:ImageButton ID="ImageButton1" runat="server" OnCommand="MyCommand" CommandName="AddToCart" CommandArgument='<%#Eval("YourID")%>' />
C#
protected void MyCommand(object sender, CommandEventArgs e)
{
var myAction = e.CommandName as string;
var myID = int.Parse(e.CommandArgument);
}