当前位置:编程学习 > C#/ASP.NET >>

为何在Repeater中使用ImageButton会莫名其妙的报错

我在Repeater的ItemTemplate中添加了ImageButton控件,代码如下:
<ItemTemplate>
 <asp:ImageButton ID="ImageButtonDelete" runat="server" ImageUrl="~/Picture/b-delete.gif" 
ToolTip="移除此商品" CommandName="Delete" CommandArgument='<%# Eval("ProductID") %>' />
</ItemTemplate>

后台cs代码:
protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            var productID = new Guid(e.CommandArgument.ToString());
            var ds = this.Profile.ShoppingCarData;
            if (ds.ShoppingCar.FindByProductID(productID) != null)
            {
                ds.ShoppingCar.RemoveShoppingCarRow(ds.ShoppingCar.FindByProductID(productID));
            }
            this.Profile.Save();
            BindDataFromProfile();
        }
    }

想实现在Repeater中点击ImageButton该删除改行,
但是运行后会报
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。 
换成Button也一样,但是换成LinkButton就可以
但是我现在需要图片显示,所以还不能用LinkButton,我看别人的项目中直接用ImageButton就可以,但我这儿就不行,网上也有很多人遇到这种情况,至今还没有解决,希望大神帮忙。谢谢 --------------------编程问答-------------------- 那就把 LinkButton 加上图片的背景样式不就得了 --------------------编程问答--------------------
能否给一个可以解决的办法,还是想用ImageButton,还是把这个问题搞懂为好 --------------------编程问答-------------------- 改成
<asp:ImageButton ID="ImageButtonDelete" runat="server" ImageUrl="~/Picture/b-delete.gif" ToolTip="移除此商品"   CommandArgument='<%# Eval("ProductID") %>'  onclick="ImageButtonDelete_Click"/>

protected void ImageButtonDelete_Click(object sender, EventArgs e)

var productID = new Guid(e.CommandArgument.ToString()); 
var ds = this.Profile.ShoppingCarData; 
if (ds.ShoppingCar.FindByProductID(productID) != null)
{
ds.ShoppingCar.RemoveShoppingCarRow(ds.ShoppingCar.FindByProductID(productID));

this.Profile.Save(); BindDataFromProfile(); 
}
--------------------编程问答-------------------- 也可以改成下面
1、Page EnableEventValidation="true"改成 false 
2、看看是否多了form或者少了form一个页面只能有一个Form,仔细检查代码就可以解决。
--------------------编程问答--------------------
引用 3 楼 kongwei521 的回复:
改成
<asp:ImageButton ID="ImageButtonDelete" runat="server" ImageUrl="~/Picture/b-delete.gif" ToolTip="移除此商品"   CommandArgument='<%# Eval("ProductID") %>'  onclick="ImageButtonDelete_Click……

不行,还是报一样的错误
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,