repeater 控件

1.基本模型

<asp:Repeater ID="rptPurchaseOrderReportForms" OnItemDataBound="rptPurchaseOrderReportForms_ItemDataBound" runat="server">
        <HeaderTemplate>
            <table class="table" id="table" border="0" cellpadding="0" cellspacing="0" style="text-align: center">
                <tr style="white-space: nowrap">
                    <td class="tbheader">
                        <%=GetText("会员下单日期")%>
                    </td>
                    <td class="tbheader">
                        <%=GetText("客户")%>
                    </td>  
                </tr>                
        </HeaderTemplate>
        <ItemTemplate>
            <tr class="tbrow">
                <td class="tbpost">
                   <%#Eval("CustomerOrderCreatedOn", "{0:yyyy-MM-dd}")%> //格式话日期
                </td>
                <td class="tbpost">
                    <%#Eval("CustomerCode")%>
                </td>              
            </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
            <tr class="tbrowalter">
                <td class="tbpost">
                    <%#Eval("CustomerOrderCreatedOn", "{0:yyyy-MM-dd}")%>
                </td>
                <td class="tbpost">
                    <%#Eval("CustomerCode")%>
                </td>           
            </tr>
        </AlternatingItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>

  2.事件

   2.1 ItemDataBound事件:当repeater绑定数据时,绑定一条触发一次。

 //例一
void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) { // This event is raised for the header, the footer, separators, and items. // Execute the following logic for Items and Alternating Items. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { if (((Person)e.Item.DataItem).name== "Good") { ((Label)e.Item.FindControl("nameLabel")).Text= "<b>***Good***</b>"; } } }

//例二:把列值为“是”的行,特定几个字段显示红色。(ItemDataBound 绑定一次触发一次)
protected void rptPurchaseOrderReportForms_ItemDataBound(object sender, RepeaterItemEventArgs e) {
// 去掉repeat的头部 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { var row = e.Item.DataItem as DataRowView; //(as 也为强转,当转换不过时返回null给row) // var row = (DataRowView)(e.Item.DataItem); //(强转,当转换不过来时报错) if (row["IsExtraPay"].ToString() == "是") { (e.Item.FindControl("lblPurchasedOn") as Label).ForeColor = System.Drawing.Color.Red; (e.Item.FindControl("lblOutTradeSN") as Label).ForeColor = System.Drawing.Color.Red; (e.Item.FindControl("lblShopName") as Label).ForeColor = System.Drawing.Color.Red; } } }

  


  2.2 ItemCommand事件:当触发某一项的某一列时,触发。

 

rpt前台: 

<asp:LinkButton ID="lbnInfo" runat="server" CommandArgument='<%# Eval("Type_ID") %>'CommandName="Info">查看</asp:LinkButton>

prt后台:

protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)

{
    if(e.CommandName == "Info")
    {
       Response.Redirect("DictionaryInfo.aspx?type_id="+e.CommandArgument.ToString());
    }
}

 3. 选择被checkbox选中的行,并获取选中行的特定值

    前台代码:           
<ItemTemplate> <tr class="tbrow"> <td class="tbpost"><asp:CheckBox ID="cbx" runat="server"/></td> <td class="tbpost"><asp:Label runat="server" ID="lblShareCode" Text='<%#Eval("ShareCode") %>'></asp:Label></td> //要被获取的值 <td class="tbpost"><%#Eval("ShareName") %></td> <td class="tbpost"><asp:Label runat="server" ID="lblMarketCode" Text='<%#Eval("MarketCode") %>'></asp:Label></td> //要被获取的值 <td class="tbpost"><%#Eval("MarketName") %></td> <td class="tbpost"><%#GetIsBlack(Eval("IsBlackList"))%></td> </tr> </ItemTemplate>
后台代码:
protected void btnBlack_Click(object sender, EventArgs e) { string shareCodes = ""; string marketCodes = ""; foreach (RepeaterItem item in rptvirtualShare.Items) //遍历repeat所有行 { if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) { var cbx = item.FindControl("cbx") as CheckBox; //选取Id为cbx的CheckBox if (cbx.Checked) { var lbl_shareCode = item.FindControl("lblShareCode") as Label; //选取id为lblShareCode的Label shareCodes += lbl_shareCode.Text + ";"; var lbl_marketCode = item.FindControl("lblMarketCode") as Label; marketCodes += lbl_marketCode.Text + ";"; } } } if (scp != null) { msg.Show("设置成功!"); } else { msg.Show("设置失败!请重新尝试!"); } }

  3.合并repeat的表格 (转至:http://www.cnblogs.com/gdjlc/p/3491102.html)

(效果图)

 

(代码)

前台代码

<asp:Repeater runat="server" ID="rptList">         
<HeaderTemplate>
   <table width="100%" border="1" cellpadding="4" cellspacing="0" bgcolor="#464646" style="border-collapse:collapse; padding:0; margin:0">  
       <tr bgcolor="#eeeeee">  
           <td>序号</td> 
           <td>类型</td>
           <td>操作</td>
           <td>编号</td>                                        
       </tr>   
</HeaderTemplate>                 
  <ItemTemplate>
   <tr bgcolor="#ffffff">                                   
           <td><%# Eval("rn") %></td>
           <td runat="server" id="td1"><%#Eval("Col1")%></td>
           <td runat="server" id="td2"><%#Eval("Col2")%></td>
           <td><%#Eval("Col3")%></td> 
           <td><%#Eval("Col4")%></td> 
       </tr>
  </ItemTemplate>   
  <FooterTemplate>
     </table>  
  </FooterTemplate>                       
</asp:Repeater>

 (后台代码)

private void BindData()
{
    rptList.DataSource = null; //这里为数据源,省略...
    rptList.DataBind();  
    MergeCell("td1", "td2");
}
private void MergeCell(string tdIdName1, string tdIdName2)
{
    for (int i = rptList.Items.Count - 1; i > 0; i--)
    {
        MergeCellSet(tdIdName1, tdIdName2, i);
    }
}
private void MergeCellSet(string tdIdName1, string tdIdName2, int i)
{
    HtmlTableCell cellPrev = rptList.Items[i - 1].FindControl(tdIdName1) as HtmlTableCell;
    HtmlTableCell cell = rptList.Items[i].FindControl(tdIdName1) as HtmlTableCell;
    cell.RowSpan = (cell.RowSpan == -1) ? 1 : cell.RowSpan;
    cellPrev.RowSpan = (cellPrev.RowSpan == -1) ? 1 : cellPrev.RowSpan;
    if (cell.InnerText == cellPrev.InnerText)
    {
        cell.Visible = false;
        cellPrev.RowSpan += cell.RowSpan;
 
        //关键代码,再判断执行第2列的合并单元格方法
        if (tdIdName2 != "") MergeCellSet(tdIdName2, "", i);
    }
}         

 

posted @ 2014-04-02 13:11  飞翔.  阅读(171)  评论(0编辑  收藏  举报