如何在页面回发后循环转发器数据项

时间:2013-01-17 13:25:08

标签: asp.net c#-4.0 repeater

我在转发器中有一个按钮,我想在此按钮单击事件中循环转发器的数据项。这是我的代码:

 protected void btnPromotionButton_OnClick(object sender, EventArgs e)
    {
        try
        {
            foreach (RepeaterItem item in rptPromotionProducts.Items)
             {
               if (item.DataItem == null)
                {
                    continue;
                 }...

这是我的转发器:

 <asp:Repeater ID="rptPromotionProducts" runat="server" OnItemDataBound="rptPromotionProducts_OnItemDataBound">
     <ItemTemplate>
        <li>
         <asp:HyperLink runat="server" ID="hlProduct" CssClass="product_image">
         <asp:Image runat="server" ID="imgWindow" />
           </asp:HyperLink>
              <div class="product_info">
              <h3>
               <a href="#">
                  <%# Eval("Name") %></a></h3>
                  </div>
                   <div>
                   <asp:Button ID="btnPromotionButton" runat="server" Text="Sepete Ekle" OnClick="btnPromotionButton_OnClick" />
                   </div>
                  </div>
              </li>
             </ItemTemplate>
        </asp:Repeater>

item.DataItem总是为null,但是repeater有行。这可能在页面后发布和返回后循环吗?

1 个答案:

答案 0 :(得分:3)

已经回答了类似问题herehere

这个问题的简短答案是否定的。回发后无法访问DataItem。

检索数据的替代方法是使用隐藏字段,链接到Repeater的DataSource或包含数据的会话对象。