Question

I am building a website whereby people, before checking out of the shopping cart (and transferring to the payment iframe) can select which items from the shopping cart list to delete. The results from the shopping card are listed in a Repeater control. There is a Button in the Repeater which deletes a record from the database (used LINQ to SQL to do that.)

The problem is that the ItemCommand event doesn't fire when i click the button. I tried response.write(test) and it still would not work.

It is as if the repeater cannot interact with the commands. It does render the results though.

  <asp:Repeater ID="RepeaterKoshnichka" 
       runat="server" OnItemCommand="RepeaterKoshnichka_ItemCommand"
  DataSourceID="LinqDataSource1">

  <ItemTemplate>
  <tr>
  <td background="images/message-bar.gif">
  <div class="message_head" style="float:left"><cite>Производ: <asp:Label ID="lblProizvod" CssClass="red_tx" Text='<%# Eval("Proizvod") %>' runat="server"></asp:Label> / Тип на Претплата: <asp:Label ID="lblPretplata" runat="server" Text='<%# Eval("Tip") %>' CssClass="red_tx"></asp:Label></cite></div>

  <div class="message_head" style="float:right"><cite>Цена: <asp:Label ID="lblCena" CssClass="red_tx" Text='<%# Eval("Cena") %>' runat="server"></asp:Label>&nbsp;

  <asp:Button ID="Button2" CssClass="main_tx" CommandName="Delete" CommandArgument='<%# Eval("NDetID") %>' runat="server" Text="Отстрани" /></cite>  
  </div>  
  </td> 
  </tr>  
  </ItemTemplate>  
  </asp:Repeater>

  protected void RepeaterKoshnichka_ItemCommand(object source, RepeaterCommandEventArgs e)
  {
     if (e.CommandName == "Delete")
     {
        if (Request.Form[e.CommandArgument.ToString()] != null)
        {  
           if (Page.User.Identity.IsAuthenticated)
           { 
               var nar = new DataClasses1DataContext();  
               Guid detnar = new Guid(e.CommandArgument.ToString());

               var query = from c in nar.Naracka_Dets 
                           where c.NDetID == detnar
                           select c;

               foreach (var c in query) 
               { 
                 nar.Naracka_Dets.DeleteOnSubmit(c);
               }
               nar.SubmitChanges();
               lblSuma.Text = ((Button)e.CommandSource).ToString();
           }
        }
     }
  }
Was it helpful?

Solution

Likely an <asp:GridView> would be a better server control for what you're working on.

As an aside, consider making a small change to your code. To help make it more readable, combine your 3 conditions into one if:

 if (e.CommandName == "Delete" &&
     Request.Form[e.CommandArgument.ToString()] != null &&
     Page.User.Identity.IsAuthenticated)
 {
     //delete things.
 }

OTHER TIPS

If you are not tied to using a Repeater you should switch to DataGrid and use a ButtonColumn for this feature - it will make life easier for you handling Item events.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top