• Repeater使用技巧


    一、在ItemDataBound事件里面动态改变Repeater控件里面的html元素

    如:

            <asp:Repeater ID="Repeater1" runat="server"
                onitemdatabound="Repeater1_ItemDataBound">
                <ItemTemplate>
                    <table>
                        <tr id="row" runat="server">
                            <td>
                                <%#DataBinder.Eval(Container.DataItem,"title")%>
                            </td>
                        </tr>

                        <tr id="row2" runat="server">
                            <td>
                             </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:Repeater>

    后台程序
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            int i = e.Item.ItemIndex;
            if (i % 2 == 0)
            {
                HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("row");
                if (row != null)
                {
                    row.BgColor = System.Drawing.Color.Black.ToString();
                }
            }

            HtmlTableRow row2 = (HtmlTableRow)e.Item.FindControl("row2");
            if (row2 != null)
            {
                 //在这里就可以对row2这个html的元素进行各种各样的赋值了
             }
        }

    二、在Repeater控件里面嵌套Repeater控件
        
    <asp:Repeater runat="server" ID="rp1" OnItemDataBound="rp1_ItemDataBound">
            
    <ItemTemplate>
                
    <tr>
                    
    <td  >
                         <%#DataBinder.Eval(Container.DataItem,"title")%>

                    
    </td>
                
    </tr>
                
    <tr>
                    
    <td>
                        
    <div  >
                            
    <asp:Repeater runat="server" ID="rp1">
                                
    <ItemTemplate>

                                    <%#DataBinder.Eval(Container.DataItem,"title2")%>
                                  
    </ItemTemplate>
                            
    </asp:Repeater>
                        
    </div>
                    
    </td>
                
    </tr>
            
    </ItemTemplate>
        
    </asp:Repeater>

    CS代码

            private void Bind()
            {  ……

                this.rptypelist.DataSource = dt;
                
    this.rptypelist.DataBind();
            }
            
    protected void rp1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {

    //判断里层repeater处于外层repeater的哪个位置( AlternatingItemTemplate,FooterTemplate,HeaderTemplate,,ItemTemplate,SeparatorTemplate)
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater rep 
    = e.Item.FindControl("rp2"as Repeater;//找到里层的repeater对象
                    DataRowView rowv = (DataRowView)e.Item.DataItem;//找到外层Repeater关联的数据项 
                    int title= Convert.ToInt32(rowv["title"]); 

                   ……
                    rep.DataSource = dt2;

                    rep.DataBind();
                }
            }

    三、添加序号的4种方法

    1、<itemtemplate>
    <tr><td>
    <%# Container.ItemIndex + 1%>
    </td></tr>
    </itemtemplate>
    2、<itemtemplate>
    <tr><td>
    <%# this.rpResult.Items.Count + 1%>
    </td></tr>
    </itemtemplate>

    3、在<form></form>中添加<Label ID="dd" ></Label>

    <body nload="show()">

    <Script. Language="JScript">
          function show()
       {
    var bj = document.all.tags("LABEL");
    for (i=0;i<obj.length;i++)
    {
    document.all["dd"][i].innerHTML=i+1;
    }
       }
       </script>

    4、后台实现方法
    后台实现方法:

    在.aspx里添加<asp:Label id="Label1" Runat="server"></asp:Label>

    在.cs里添加

    ** void InitializeComponent()
    {   
       this.Repeater1.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound);
       this.Load += new System.EventHandler(this.Page_Load);

    }

    ** void Repeater1_ItemDataBound(object source, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
       {
        ((Label)e.Item.FindControl("Label1")).Text = Convert.ToString(e.Item.ItemIndex + 1);
       }
    }

  • 相关阅读:
    从锅炉工到AI专家(1)
    【Offer】[47] 【礼物的最大价值】
    【Offer】[46] 【把数字翻译成字符串】
    【Offer】[45]【把数组排成最小的数】
    【Offer】[44] 【数字序列中某一位的数字】
    【Offer】[43] 【1~n整数中1出现的次数】
    【Offer】[42] 【连续子数组的最大和】
    【Offer】[41] 【数据流中的中位数】
    【Offer】[40] 【最小的K个数】
    【Offer】[39] 【数组中出现次数超过一半的数字】
  • 原文地址:https://www.cnblogs.com/linzheng/p/1858983.html
Copyright © 2020-2023  润新知