简体   繁体   中英

ItemCommand event doesn't fire with the Repeater control

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 tho.

I would really appreciate if you could help me as I'm approaching a deadline and I've exhausted all the resources on the internet before turning to you guys!

Here's the code:

<asp:Repeater ID="RepeaterKoshnichka" runat="server" 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();
            }
        }
    }
}

Your help is greatly appreciated!

您需要添加onitemcommand事件处理程序链接,即

OnItemCommand="RepeaterKoshnichka_ItemCommand"

<asp:Button UseSubmitBehavior =“ False” ... />

需要注意的一件事是,在绑定时,请确保仅在未回发(或处理完事件后)的情况下才进行绑定,否则会丢失该事件。

I had a smimilar issue and nothing solved my problem. It ends up (after searching for a very long time) that I found that I have a textfield way higher on the page that was not validating, so I just added CausesValidation="false" to the button in the repeater and problem solved.

Is the post back happening for any other controls on your page? Do you have any javascript on your page like "javascript:void(0)" ?

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