Repeater控件使用

对于Repeater控件,主要是注意HeaderTemplate、ItemTemplate、AlternatingItemTemplate以及FooterTemplate的用法。

1、前台代码
<%@ Page Language="C#" AutoEventWireup="true" Inherits="_Default1" CodeBehind="Default.aspx.cs" %>
 
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>商品类别列表</title>
    <script language="javascript" type="text/javascript">
        //-------------------------------------------------------------
        // Select all the checkboxes (Hotmail style)
        //-------------------------------------------------------------
 
        function SelectAllCheckboxes(spanChk) {
            var xState = spanChk.checked;
 
            elm = spanChk.form.elements;
            for (i = 0; i < elm.length; i++) {
                if (elm[i].type == "checkbox" && elm[i].id != spanChk.id) {
                    if (elm[i].checked != xState)
                        elm[i].click();
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        欢迎您的登录:</div>
    <div>
        <asp:Repeater ID="TopList" runat="server">
            <HeaderTemplate>
                <table cellspacing="0" border="1" style=" 100%; text-align: center; border: 1px solid #ccc;
                    border-top: 2px solid #ccc; font-size: 12px;" class="dataTable">
                    <tr style="background-color: #F2F2F2;" class="dataTableHead">
                        <td align="center" width="15">
                        </td>
                        <td align="center" width="34">
                            <font style="font-weight: bold">序号</font>
                        </td>
                        <td align="center">
                            <font style="font-weight: bold">类别名称</font>
                        </td>
                        <td align="center">
                            <font style="font-weight: bold">管理操作</font>
                        </td>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr onmouseover="this.className='mouseoverstyle'" onmouseout="this.className='mouseoutstyle'"
                    style="cursor: hand;">
                    <td height="22" style="border: 1px solid #ccc;">
                        <input id="TopID" runat="server" value='<%#((DataRowView)Container.DataItem)["ProductCategory_ID"]%>'
                            type="hidden" />
                        <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="false"></asp:CheckBox>
                    </td>
                    <td height="22" style="border: 1px solid #ccc;">
                        <%#Container.ItemIndex + 1%>
                    </td>
                    <td height="22" style="border: 1px solid #ccc;">
                        <%#((DataRowView)Container.DataItem)["ProductCategory_Name"]%>
                    </td>
                    <%--                                <td height="22" style="border: 1px solid #ccc;">
                                    <%#((DataRowView)Container.DataItem)["Student_Age"]%>
                                </td>
                                <td height="22" style="border: 1px solid #ccc;">
                                    <%# DataBinder.Eval(Container.DataItem, "Student_Birth", "{0:d}")%>
                                </td>--%>
                    <td height="22" style="border: 1px solid #ccc;">
                        <a href='Show.aspx?id=<%#((DataRowView)Container.DataItem)["ProductCategory_ID"]%>'
                            title="查看详情">[查看详情]</a> | <a href='Modify.aspx?id=<%#((DataRowView)Container.DataItem)["ProductCategory_ID"]%>'
                                title="修改数据">[修改数据]</a>
                    </td>
                </tr>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <tr onmouseover="this.className='mouseoverstyle'" onmouseout="this.className='mouseoutstyle'"
                    style="cursor: hand;">
                    <td height="22" style="background-color: #F5F7F8; border: 1px solid #ccc;">
                        <input id="TopID" runat="server" value='<%#((DataRowView)Container.DataItem)["ProductCategory_ID"]%>'
                            type="hidden" />
                        <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="false"></asp:CheckBox>
                    </td>
                    <td height="22" style="background-color: #F5F7F8; border: 1px solid #ccc;">
                        <%#Container.ItemIndex + 1%>
                    </td>
                    <td height="22" style="background-color: #F5F7F8; border: 1px solid #ccc;">
                        <%#((DataRowView)Container.DataItem)["ProductCategory_Name"]%>
                    </td>
                    <%--                                     <td height="22" style="background-color: #F5F7F8; border: 1px solid #ccc;">
                                    <%#((DataRowView)Container.DataItem)["Student_Age"]%>
                                </td>
                                <td height="22" style="background-color: #F5F7F8; border: 1px solid #ccc;">
                                    <%# DataBinder.Eval(Container.DataItem, "Student_Birth", "{0:d}")%>
                                </td>--%>
                    <td height="22" style="border: 1px solid #ccc;">
                        <a href='Show.aspx?id=<%#((DataRowView)Container.DataItem)["ProductCategory_ID"]%>'
                            title="查看详情">[查看详情]</a> | <a href='Modify.aspx?id=<%#((DataRowView)Container.DataItem)["ProductCategory_ID"]%>'
                                title="修改数据">[修改数据]</a>
                    </td>
                </tr>
            </AlternatingItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
        <asp:CheckBox ID="SelectAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server"
            Text="全选"></asp:CheckBox>&nbsp; &nbsp;
        <asp:Button ID="DeleteBTN" runat="server" CssClass="button" Text="删除选中项" OnClick="DeleteBTN_Click" />&nbsp;
        <a href="Add.aspx">新增</a><br />
        <table class="RecordTable" border="0" cellpadding="0" cellspacing="0" style=" 100%;
            border: solid 1px #ccc;">
            <tr>
                <td style=" 55%; padding: 5px; margin: 5px;" align="right">
                    <webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="100%" UrlPaging="true"
                        ShowPageIndexBox="Always" PageIndexBoxType="DropDownList" TextBeforePageIndexBox="Go To Page: "
                        HorizontalAlign="right" PageSize="2" OnPageChanged="AspNetPager1_PageChanged"
                        EnableTheming="true" CssClass="paginator" CurrentPageButtonClass="cpb">
                    </webdiyer:AspNetPager>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
2、后台代码
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using System.Text;
 
public partial class _Default1 : System.Web.UI.Page
{    
    Ced.BLL.ProductCategory bll = new Ced.BLL.ProductCategory();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
        
    }
 
    private void BindData()
    {
 
        DataSet ds = new DataSet();
        StringBuilder strWhere = new StringBuilder();
        ds = bll.GetList(strWhere.ToString());
         DataTable dt = ds.Tables[0];
        DataView dv = dt.DefaultView;
        AspNetPager1.RecordCount = dv.Count;
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = dv;
        pds.AllowPaging = true;
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
        pds.PageSize = AspNetPager1.PageSize;
        TopList.DataSource = pds;
        TopList.DataBind();
    }
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        BindData();
    }
    protected void DeleteBTN_Click(object sender, EventArgs e)
    {
        List<string> CheckList = GetCheckBoxIDs();
        for (int i = 0; i < CheckList.Count; i++)
        {
            DeleteStudent(CheckList[i].ToString());
        }
        BindData();
    }
 
    private void DeleteStudent(string p)
    {
        bll.DeleteList(p);
    }
 
    #region 得到选种的CheckBox的ID,保存到ArrayList中
    public List<string> GetCheckBoxIDs()
    {
        List<string> myArrayList = new List<string>();
        CheckBox cb = new CheckBox();
        string strID = "";
        HtmlInputHidden id;
        for (int i = 0; i < this.TopList.Items.Count; i++)
        {
            cb = (CheckBox)this.TopList.Items[i].FindControl("chkSelect");
            id = (HtmlInputHidden)this.TopList.Items[i].FindControl("TopID");
            if (cb.Checked == true)
            {
                strID = id.Value.ToString();//获取值 
                myArrayList.Add(strID);
            }
        }
        return myArrayList;
    }
    #endregion
 
}

原文地址:https://www.cnblogs.com/zhouhb/p/2909749.html