简体   繁体   English

ItemCommand事件不会与Repeater控件一起触发

[英]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. 我正在建立一个网站,人们在离开购物车(并转移到付款iframe)之前,可以从购物车列表中选择要删除的项目。 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.) 转发器中有一个按钮,用于从数据库中删除一条记录(使用LINQ to SQL来做到这一点。)

THe problem is that the ItemCommand event doesn't fire when i click the button. 问题是,当我单击按钮时,ItemCommand事件不会触发。 I tried 'response.write(test)' and it still would not work. 我尝试了“ response.write(test)”,但仍然无法正常工作。 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! 如果在临近截止日期之前您能为我提供帮助,并且在联系你们之前,我已经耗尽了Internet上的所有资源,我将不胜感激!

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. 最终(在搜索了很长时间之后),我发现页面上的文本字段没有通过验证,因此我只将CausesValidation="false"添加到转发器中的按钮上,问题就解决了。

Is the post back happening for any other controls on your page? 页面上的其他控件是否发生了回发? Do you have any javascript on your page like "javascript:void(0)" ? 您的网页上是否有任何JavaScript,例如“ javascript:void(0)”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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