简体   繁体   中英

c# ASP.Net GridView multiple custom delete Buttons

I have a grid view that inter-joins multiple tables, a shopping cart and a bins where the product is at, as well as the necessary tables to tie this information together. Is there a way to get the grid view to generate two delete buttons instead of just one?

The first will simply remove the item from the shopping cart.

The second will remove the item from a bin and reduce the shopping cart count by one.

it would be nice if these were labeled "remove from cart" and "remove from bin" respectfully

All I relay need is for it to trigger an event in the code behind file. I can take care of the rest from there

I use a Repeater for something like that instead of a GridView.

Just build the table or use CSS in your ASPX page. You DataBind the Repeater as you would do with the GridView. I would then use LinkButton in a column or div like this:

<asp:LinkButton runat="server" ID="lbtnDelete" CommandArgument='<%#"Delete_" + Eval("ProductID") %>' ClientIDMode="AutoID" CausesValidation="False">remove from cart</asp:LinkButton>

<asp:LinkButton runat="server" ID="jbtnDeduct" CommandArgument='<%#"Minus_" + Eval("ProductID") %>' ClientIDMode="AutoID" CausesValidation="False">remove from bin</asp:LinkButton>

The Repeater need the onItemCommand:

<asp:Repeater id="rpt1" runat="server" onitemcommand="rpt1_ItemCommand">

Then you get a click to that onItemCommand were you can handle the required actions:

protected void rpt1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    string sButtonID = e.CommandArgument.ToString();
    string sID = "";
    if (sButtonID.Contains("Delete_"))
    {
       sID = sButtonID.Replace("Delete_", "");
       // Add your logic
    }
    if (sButtonID.Contains("Minus_"))
    {
       sID = sButtonID.Replace("Minus_", "");
       // Add your logic
    }
 }

You could even use an ImageButton instead of a LinkButton. Show an icon for a bin and one with a minus sign.

There might be other ways, but I like this one and it works for me,

Hope this helps.

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