0

I have a question about getting object (or at least ID of an object) from a listview in asp.net when i click on the button in the listview. SelectedIndexChanged event doesnt fire. Whats other option?

My listview with 2 image buttons in it (info and shopping cart):

screenshot

My aspx code:

<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>
3
  • do you search on internet ? stackoverflow.com/questions/920381/… Commented May 24, 2016 at 17:00
  • But how to pass value from first TD and second TD? how to get content to pass the value? Commented May 24, 2016 at 17:19
  • you use a different way to make it work, or hidden input fields... Commented May 24, 2016 at 17:41

1 Answer 1

0

The button is intercepting the event, use 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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.