简体   繁体   中英

How do I pass a value to a button's click handler in a ListView?

on my page I'am showing some data from database, each item have button to remove this from database.

Here's aspx:

 <asp:ListView ID="productList" runat="server"
                DataKeyNames="id_produkt"
                ItemType="Shop.Models.Produkt" SelectMethod="GetProducts">
                <ItemTemplate>
                    <tr>
                        <td><%#:Item.id_produkt%></td>
                        <td><%#:Item.nazwa_produkt%></td>
                        <td><%#:Item.cena%></td>
                        <td><%#:Item.poprzednia_cena%></td>
                        <td><%#:Item.Ilosc%></td>
                        <td>
                            <asp:Button CssClass="btn btn-info btn-sm" ID="Edytuj" runat="server" Text="Edytuj" OnClick="AddProductButton_Click" CausesValidation="true" />
                            <asp:Button CssClass="btn btn-danger btn-sm" ID="RemoveButton" runat="server" Text="Usuń Produkt" OnClick="RemoveProductButton_Click" CausesValidation="false" />
                            <a href="AdminPage_Details.aspx?productID=<%#:Item.id_produkt%>" class="btn btn-info btn-sm">Szczegóły</a>
                        </td>
                    </tr>
                </ItemTemplate>
                <ItemSeparatorTemplate></ItemSeparatorTemplate>
            </asp:ListView>

How can i get Item.id_produkt in code-behind method?

For now it looks like this:

protected void RemoveProductButton_Click(object sender, EventArgs e)
        {
            using (var _db = new Shop.Models.EgzemplarzContext())
            {
                int productId = Convert.ToInt16();
                var myItem = (from c in _db.Produkty where c.id_produkt == productId select c).FirstOrDefault();

                IQueryable<Egzemplarz> query = _db.Egzemplarze;
                query = query.Where(p => p.id_produkt == productId);

                if (myItem != null && query != null)
                {
                    foreach ( var item in query)
                    {
                        _db.Egzemplarze.Remove(item);
                    }

                    _db.Produkty.Remove(myItem);
                    _db.SaveChanges();

                    // Reload the page.
                    string pageUrl = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count());
                    Response.Redirect(pageUrl + "?ProductAction=remove");
                }
                else
                {
                    LabelAddStatusDanger.Text = "Nie można usunąć produktu.";
                }
            }
        }

Somehow I need to get Item.id_produkt from the productlist in this method.

How can I do this ?

Pass it using the CommandArgument property of the button.

Markup:

<asp:Button ID="RemoveButton"
            runat="server"
            Text="Usuń Produkt"
            OnClick="RemoveProductButton_Click"
            CommandArgument='<%#:Item.id_produkt%>' />

Code behind:

protected void RemoveProductButton_Click(object sender, EventArgs e)
{
    var button = (Button)sender;
    var id_produkt = button.CommandArgument;
}

This sets the CommandArgument of the buttons in the ListView to have the product ID that you want. Then when the click event handler is get, the sender will be your button. We need to cast it to a button type in order to access the CommandArgument property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM