ItemCommand后ASP.NET直放站没有约束力 [英] ASP.NET Repeater not binding after ItemCommand

查看:156
本文介绍了ItemCommand后ASP.NET直放站没有约束力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环用户控件的中继器,像这样的:

I have a repeater that is looping a user control, like this:

                <asp:Repeater ID="repItems" runat="server" EnableViewState="false" 
                OnItemCommand="repItems_ItemCommand">
            <ItemTemplate>
                <dmg:confirmItem runat="server" 
                OnDataBinding="confirmitemItem_DataBinding"  
                Basket="<%# Container.DataItem %>" />
            </ItemTemplate>

            </asp:Repeater>

我的code的背后是这样的:

My code behind looks like this:

  public void BindItems(List<ShopBasket> baskets)
    {
        _baskets = baskets;
        repItems.DataSource = baskets;
        repItems.DataBind();
    }

我的自定义用户控件看起来是这样的:

My custom user control looks like this:

public ShopBasket Basket;
        protected void Page_Load(object sender, EventArgs e)
        {

            imgItem.ImageUrl = ShopImagePath + Basket.ImageFilename;
            ...etc...
        }

所有出色的作品第一次,篮下项目绑定到篮对象,一切是伟大的。

All works brilliantly the first time around, the basket items are bound to Basket objects and everything is great.

然而,当我收到ItemCommand从我的中继器,并更新购物篮内容(注:不添加或删除都是在这里完成,刚刚更新的数量),那么我重新绑定看到最新的值,BOOM!空引用 - 在用户控件的Page_Load没有篮子的对象。尽管这是通过跟踪看到()方法被调用像往常一样BindItems和篮子的存在。

However, when I receive an ItemCommand from my repeater, and update the basket contents (Note: No adding or removing is done here, just updates the quantity) then I rebind to see the latest values, and BOOM! Null reference - no Basket object in the user control Page_Load. This is despite tracing through to see that the BindItems() method is called as usual, and the Baskets are there.

我presume这有什么做的生命周期,但它有我击败了。

I presume this has something to do with the life cycle but it has me beat.

任何想法?

谢谢
邓肯

推荐答案

这是一个有点危险,有一个公共的场存储项目的约束,尤其是当你有多个项目。一个更安全的办法做到这一点是提取绑定到中继器的篮子作为的DataItem,并用它做什么的中继ItemCommand事件的方式。正如你所知道的篮子将现有的(因为它的数据绑定)的ItemDataBound会更安全,Page_Load中是不是在这里一个安全的选择...

It's a little dangerous to have a public field store an item being bound, especially when you have multiple items. A safer way to do it is to extract the Basket as the DataItem bound to the repeater, and do something with it that way in the repeater ItemCommand event. ItemDataBound would be even safer as you know the basket would be existing (since it's data bound), Page_Load is not a safe option here...

心连心。

这篇关于ItemCommand后ASP.NET直放站没有约束力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆