对于Repeater控件,主要是注意HeaderTemplate、ItemTemplate、AlternatingItemTemplate以及FooterTemplate的用法。
1、前台代码
<%@ Page Language="C#" AutoEventWireup="true" Inherits="_Default1" CodeBehind="Default.aspx.cs" %> <%@ Import Namespace="System.Data" %>商品类别列表
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) { ListCheckList = 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 GetCheckBoxIDs() { List myArrayList = new List (); 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 }