Repeater 控件不会触发 ItemCommand 事件
我正在建立一个网站,人们在结帐购物车(并转移到付款 iframe)之前可以选择从购物车列表中删除哪些项目。购物卡的结果列在 Repeater 控件中。中继器中有一个按钮,可以从数据库中删除一条记录(使用 LINQ to SQL 来执行此操作。)
问题是,当我单击该按钮时,ItemCommand 事件不会触发。我尝试了“response.write(test)”,但它仍然不起作用。就好像中继器无法与命令交互一样。它确实渲染了结果。
如果您能帮助我,我将非常感激,因为我即将截止日期,并且在求助于你们之前我已经用尽了互联网上的所有资源!
代码如下:
<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>
<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();
}
}
}
}
非常感谢您的帮助!
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>
<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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要添加 onitemcommand 事件处理程序链接,即
You need to add the onitemcommand event handler link, i.e.
<asp:Button UseSubmitBehavior="False" ... />
需要注意的一件事是,当您绑定时,请确保仅在未回发(或在处理事件之后)时才进行绑定,否则您将丢失该事件。
One thing to note, when you are binding, make sure you are only binding if NOT post back (or after you've processed the event) otherwise you'll lose the event.
您页面上的任何其他控件是否会发生回发?你的页面上有像“javascript:void(0)”这样的javascript吗?
Is the post back happening for any other controls on your page? Do you have any javascript on your page like "javascript:void(0)" ?
我遇到了类似的问题,但没有解决我的问题。最终(在搜索了很长时间之后)我发现页面上有一个未验证的文本字段,所以我只是将
CausesValidation="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.