Repeater.Items 上的 DataItem 始终为 null [英] DataItem on Repeater.Items is always null

查看:43
本文介绍了Repeater.Items 上的 DataItem 始终为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将转发器的数据源设置为列表(MyProducts 是一个简单的类,仅包含 get/setter).

I am setting the DataSource of my repeater to a List (MyProducts is a simple class, consisting of only get/setters).

在此和DataBind() 之后,我可以在调试模式下看到每个Repeater.Items 的DataItem 为空.在进行回发并尝试更新 MyProducts 时,Repeater.Items[n].DataItem 仍然为 null,我无法投射它来完成我的工作.

After this and DataBind(), I can see in debugging mode that DataItem of each Repeater.Items is null. When making a postback and trying to update MyProducts, the Repeater.Items[n].DataItem is still null and Im not able to cast it, to do my work.

当我对中继器进行数据绑定时,为什么没有在每个中继器项上设置数据项?我无法弄清楚/谷歌.我的代码的所有其他方面都正常工作(将数据从 MyProducts 输出到 aspx,例如使用:

Why isn't DataItem set on each RepeaterItem, when I databind my repeater? I cant figure/Google it out. Every other aspect of my code works correctly (outputting data from MyProducts to aspx, using for instance:

<asp:TextBox runat="server" id="q" Text='<%# DataBinder.Eval(Container.DataItem, "Quantity")%>'></asp:TextBox>

更多代码:

public class MyProducts
    {
        public string Number
        {
            get; set;
        }

        public decimal Price
        {
            get; set;
        }

        public decimal Quantity
        {
            get; set;
        }

        public decimal Total
        {
            get { return Quantity * Price; }
        }
    }

生成:

public List<MyProducts> TheProducts
{
 get { // Invoking webservice, getting response as xml and converting it to a list of MyProducts }
}

我的用户控制:

// Bind products in cart
r.DataSource = TheProducts;
r.DataBind();
// Debugging "r.Items[n].DataItem" now shows "null", eventhough all objects has been correctly binded

编辑#2,调试信息.DataSource 被正确加载,但 Repeater.Items[3].DataItem 为空.它应该有一个价值,对吧?截图

Edit #2, the debugging info. DataSource gets correctly loaded, but Repeater.Items[3].DataItem is null. It should have a value, right? screenshot

编辑#3,我现在明白了,我认为DataItem在设置DataSource时总是可以访问的,并没有想到包含完整代码(我试图在Page_Load中访问它).

Edit #3, I get it now, I thought DataItem was always accessible when DataSource is set, and did not think of including the full code (I tried to access it in Page_Load).

在用户编辑了数量值后,我想为 MyProducts 保存新数量.我通过放置一个包含 MyProducts id 的隐藏字段解决了这个问题,这样我就可以手动查找它并从那里获取 MyProducts 对象.

After the user has edited an quantity value, I wanted to save the new quantity for a MyProducts. I solved it by placing a hiddenfield which holds the id of MyProducts, so that I can manually look it up and get the MyProducts object from there.

感谢 emremp、Mark Avenius 和所有其他参与的人.

Thanks to emremp, Mark Avenius and all others who pitched in.

推荐答案

您需要整个列表的目的是什么?页面渲染后,不会保留Repeater绑定的列表.如果您需要保留它,您可以将其放入会话并根据需要检索它(例如在 Page_Load 上):

For what purpose do you need the entire list? After the page is rendered, the list to which the Repeater is bound is not retained. If you need to keep it, you can put it into the session and retrieve it as necessary (on Page_Load, e.g.):

private List<MyProducts> _myList;
protected void Page_Load(object sender, EventArgs e)
{
    _myList = Session[MYPRODUCTSKEY] as IList;
}

您也可以将其放入您的 getter(首先检查会话,并在必要时调用网络服务):

You could also put this into your getter (check the session first, and invoke the webservice if necessary):

public List<MyProducts> TheProducts
{
 get 
 { 
     if(Session[MYPRODUCTSKEY] == null)
         Session[MYPRODUCTSKEY] = //invoke webservice
     return Session[MYPRODUCTSKEY] as List<MyProducts>;
 }
}

这篇关于Repeater.Items 上的 DataItem 始终为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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