• Repeater控件的记忆碎片


    Repeater 控件用于显示重复的信息,这些信息被绑定在该控件上。一般项目中经常出现三种使用方式,
    方式一
    在aspx页面,写好需要循环输出的内容,一般包含用户自定义控件、服务器控件、Html格式的片段、和<%# Eval("Name")%>这种方式来动态显示获取到得数据列表:

    <asp:Repeater ID="rpImage" runat="server">
        <ItemTemplate>   
            <li>
                <a href="<%# (Container.DataItem as ProductImage).ResourceUrl%>" class="<%# Container.ItemIndex == 0 ? "currImg " : "" %>">
                    <img src="<%# (Container.DataItem as ProductImage).ResourceUrl%>"
                         class="<%# (Container.DataItem as ProductImage).ImageVersion)%>">
                    <%# Eval("Name").ToString() %>
                </a>
            </li>
        </ItemTemplate>
    </asp:Repeater>

    在cs文件,是用GetProductImageList方法来获取List<ProductImage>类型的数据列表,并绑定在Repeater控件上面:
    上面的不包含用户自定义控件、服务器控件,所以不需要ItemDataBound事件来对单个的数据项进行个性化的赋值

    protected override void BindDataSource()
    {
        this.rpImage.DataSource = GetProductImageList();
        this.rpImage.DataBind();
    }

    方式二
    在aspx页面,这次包含了用户自定义控件,所以需要用到ItemDataBound事件来对列表中的每一个用户自定义控件进行个性化的赋值,用户自定义控件可以有公用的方法或者属性,

    让我们在ItemDataBound事件中赋值:

    <asp:Repeater ID="gvItemList" runat="server" EnableViewState="false">
        <ItemTemplate>
             <li>
                 <UCCommon:ImageCell ID="imageCell" runat="server" />
                 <a href="###" title='<%# Eval("Name").ToString() %>' href='<%# Eval("Code").ToString()%>'>
                     <UCCommon:ProductFullNameCell ID="productFullNameCell" runat="server" />
                 </a>
                 <UCCommon:UCProductControlCell ID="productControlCell" runat="server"/>
             </li>
        </ItemTemplate>
    </asp:Repeater>

    在cs文件,用户自定义控件可以有公用的方法或者属性,让我们在ItemDataBound事件中赋值:

    protected override void BindDataSource()
    {
        this.gvItemList.DataSource = productList;
        this.gvItemList.DataBind();
    }
    protected override void OnInit(EventArgs e)
    {
        this.gvItemList.ItemDataBound += new RepeaterItemEventHandler(this.OnItemListDataBound);
        base.OnInit(e);
    }
    private void OnItemListDataBound(object sender, RepeaterItemEventArgs e)
    {

        ProductCellInfo productItem = (ProductCellInfo)e.Item.DataItem;

        if (productItem != null)
        {
           ProductFullNameCell productName;
           ImageCell image;
           ProductControlCell productControlCell;

           foreach (Control sub in e.Item.Controls)
           {

               productName = sub as ProductFullNameCell;
               if (productName != null)
               {
                   productName.InitProductFullName(productItem.Title, productItem.PromotionTitle, DispalyContentLength);
                   continue;
               }

               image = sub as ImageCell;
               if (image != null)
               {
                   image.InitImageCell2(productItem.ID, productItem.Code, productItem.Name, productItem.ImageUrl, productItem.ImageVersion);
                                   
                   continue;
               }

               productControlCell = sub as ProductControlCell;
               if (productControlCell != null)
               {
                   productControlCell.InitProductControlCell(productItem);
                   continue;
               }
           }
       }
    }

    方式三:
    在aspx页面,可以显示设置OnItemDataBound属性,就不用像方式二那样,在cs文件中的OnInit方法中动态绑定,代码如下:

    <asp:Repeater ID="rptListCell" runat="server" OnItemDataBound="RptAllOnItemDataBound">
        <ItemTemplate>
            <li>
               <a href='<%#Eval("ID"))>' title='<%#Encode(Eval("Name")) %>'>
                    <span><%#Encode(Eval("Name")) %></span>
                    <asp:PlaceHolder ID="pNew" runat="server" Visible="false"></asp:PlaceHolder>
                    <asp:PlaceHolder ID="pHot" runat="server" Visible="false"></asp:PlaceHolder>
                    <asp:Literal ID="literalValidGiftOption" runat="server"></asp:Literal>
            </a>
            </li>
        </ItemTemplate>
    </asp:Repeater>

    在cs文件:

    protected override void BindDataSource()
    {
        base.BindDataSource();

        this.rptListCell.DataSource = this.List;
        this.rptListCell.DataBind();
    }

    protected void RptAllOnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        CategoryInfo category = (CategoryInfo)e.Item.DataItem;
        PlaceHolder pHot = e.Item.FindControl("pHot") as PlaceHolder;
        PlaceHolder pNew = e.Item.FindControl("pNew") as PlaceHolder;
        Literal lit = e.Item.FindControl("literalValidGiftOption") as Literal;
        switch (category.PromotionStatus)
        {
            case "H":
               pHot.Visible = true;
               break;
            case "N":
               pNew.Visible = true;
               break;
            default:
               break;
        }
        lit.Text = category.Name;
    }


     

  • 相关阅读:
    解决Unsupported major.minor version 51.0问题的感悟
    python 自己实现for循环:
    去除(UTF8)格式文本中的Bom
    python range与xrange
    Permission denied: make_sock: could not bind to address处理
    This Android SDK requires Android Developer Toolkit version 20.0.0 or above
    centos下postgresql的安装与配置 20101217 12:39:15
    android软键盘 android:windowSoftInputMode
    android 代码实现安装卸载apk
    Android有效解决加载大图片时内存溢出的问题
  • 原文地址:https://www.cnblogs.com/lmfeng/p/2381704.html
Copyright © 2020-2023  润新知