最近一个项目用到repeater,所以收集了和它的基本使用方法,挺简单的,供大家共享
注意:如果有如果有更新功能,其实和删除功能是一样的,只需要在服务器端找到所选的要编辑的项目就可以了
或者是在每条记录后面加上编辑项,再加上链接就可以了'编辑
最近一个项目用到repeater,所以收集了和它的基本使用方法,挺简单的,供大家共享
注意:如果有如果有更新功能,其实和删除功能是一样的,只需要在服务器端找到所选的要编辑的项目就可以了
或者是在每条记录后面加上编辑项,再加上链接就可以了<a href='update.aspx?id=<%#Eval("ID")%>'>编辑</a>
以下是前台页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater.aspx.cs" Inherits="Repeater" %>
![](/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Repeater控件使用</title>
<script language="javascript" type="text/javascript">
function Check(parentChk,ChildId)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var oElements = document.getElementsByTagName("INPUT");
var bIsChecked = parentChk.checked;
for(i=0; i<oElements.length;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( IsCheckBox(oElements[i]) &&
IsMatch(oElements[i].id, ChildId))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
oElements[i].checked = bIsChecked;
}
}
}
function IsMatch(id, ChildId)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var sPattern ='^Repeater1.*'+ChildId+'$';
var oRegExp = new RegExp(sPattern);
if(oRegExp.exec(id))
return true;
else
return false;
}
function IsCheckBox(chk)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(chk.type == 'checkbox') return true;
else return false;
}
![](/Images/OutliningIndicators/None.gif)
</script>
</head>
![](/Images/OutliningIndicators/None.gif)
<body>
<form id="form1" runat="server">
<div style="margin-bottom:20px;text-align:center; width:1006px;">Repeater控件使用</div>
<asp:Repeater ID="Repeater1" runat="server">
<%--SeparatorTemplate描述一个介于每条记录之间的分隔符--%>
<%--<SeparatorTemplate>
<tr>
<td colspan="5"><hr /></td>
</tr>
</SeparatorTemplate>--%>
<HeaderTemplate>
<table border="1" cellpadding="0" cellspacing="0" style="width:1006px;border-collapse:collapse; text-align:center;">
<tr>
<td style="background-color:#cccccc; font-weight:bold; height:25px;"><input id="chkAll" name="chkAll" runat="server" type="checkbox" onclick="Check(this,'chkItem')" title="全选" />全</td>
<td style="background-color:#cccccc; font-weight:bold; height:25px;">View</td>
<td style="background-color:#cccccc; font-weight:bold; height:25px;">CustomerID</td>
<td style="background-color:#cccccc; font-weight:bold;">CompanyName</td>
<td style="background-color:#cccccc; font-weight:bold;">ContactName</td>
<td style="background-color:#cccccc; font-weight:bold;">ContactTitle</td>
<td style="background-color:#cccccc; font-weight:bold;">Address</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:CheckBox ID="chkItem" runat="server" /></td>
<td><a href='<%# "View.aspx?id="+DataBinder.Eval(Container.DataItem, "CustomerID") %>' target="_blank">View</a></td>
<td><asp:Label ID="lblID" Text='<%# DataBinder.Eval(Container.DataItem, "CustomerID")%>' runat="server"></asp:Label></td>
<td><%# DataBinder.Eval(Container.DataItem, "CompanyName")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "ContactName")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "ContactTitle")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "Address")%></td>
</tr>
</ItemTemplate>
<%--AlternatingItemTemplate描述交替输出行的另一种外观--%>
<AlternatingItemTemplate>
<tr bgcolor="#e8e8e8">
<td><asp:CheckBox ID="chkItem" runat="server" /></td>
<td><a href='<%# "View.aspx?id="+DataBinder.Eval(Container.DataItem, "CustomerID") %>' target="_blank">View</a></td>
<td><asp:Label ID="lblID" Text='<%# DataBinder.Eval(Container.DataItem, "CustomerID")%>' runat="server"></asp:Label></td>
<td><%# DataBinder.Eval(Container.DataItem, "CompanyName")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "ContactName")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "ContactTitle")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "Address")%></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<div style="background-color:#dedede; width:1006px;">
<asp:Button ID="btnDel" runat="server" Text="删除" OnClick="btnDel_Click" />
<asp:label ID="lblCurrentPage" runat="server"></asp:label>
<asp:HyperLink id="lnkFrist" runat="server">首页</asp:HyperLink>
<asp:HyperLink id="lnkPrev" runat="server">上一页</asp:HyperLink>
<asp:HyperLink id="lnkNext" runat="server">下一页</asp:HyperLink>
<asp:HyperLink id="lnkEnd" runat="server">尾页</asp:HyperLink>
</div>
</form>
</body>
</html>
下面是后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
![](/Images/OutliningIndicators/None.gif)
public partial class Repeater : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
PagedDataSource PDS = new PagedDataSource();
private string ConnStr = ConfigurationManager.AppSettings["dbConnectionString"];
private string strsql = "";
![](/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!this.Page.IsPostBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.bind();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private void bind()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int TotalCount = 0;//总记录数
int TotalPage = 1; //总页数
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select CustomerID,CompanyName,ContactName,ContactTitle,Address from Customers order by CustomerID desc", conn);
DataSet ds = new DataSet();
da.Fill(ds, "Customers");
DataView dv = ds.Tables[0].DefaultView;
![](/Images/OutliningIndicators/InBlock.gif)
TotalCount = dv.Count;
PDS.DataSource = dv;
conn.Close();
PDS.AllowPaging = true;
PDS.PageSize = 20;
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;
![](/Images/OutliningIndicators/InBlock.gif)
if (TotalCount == 0)
TotalPage = 1;
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (TotalCount % PDS.PageSize == 0)
TotalPage = TotalCount / PDS.PageSize;
else
TotalPage = TotalCount / PDS.PageSize + 1;
}
![](/Images/OutliningIndicators/InBlock.gif)
PDS.CurrentPageIndex = CurPage-1;
lblCurrentPage.Text = "共" + TotalCount.ToString() + "条记录 当前页:" + CurPage.ToString() + "/" + TotalPage;
![](/Images/OutliningIndicators/InBlock.gif)
lnkFrist.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
if (!PDS.IsFirstPage)
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
![](/Images/OutliningIndicators/InBlock.gif)
if (!PDS.IsLastPage)
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
lnkEnd.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + TotalPage;
![](/Images/OutliningIndicators/InBlock.gif)
Repeater1.DataSource=PDS;
Repeater1.DataBind();
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void btnDel_Click(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string ID = "";
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < this.Repeater1.Items.Count; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CheckBox cbox = (CheckBox)this.Repeater1.Items[i].FindControl("chkItem");
if (cbox.Checked == true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ID == "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ID = "'"+((Label)this.Repeater1.Items[i].FindControl("lblID")).Text+"'";
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ID += "," + "'" + ((Label)this.Repeater1.Items[i].FindControl("lblID")).Text + "'";
}
}
}
strsql = "delete from Customers where CustomerID in (" + ID + ")";
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlConnection conn = new SqlConnection(ConnStr);
SqlCommand comm = new SqlCommand(strsql, conn);
comm.Connection.Open();
comm.ExecuteNonQuery();
comm.Connection.Close();
System.Web.HttpContext.Current.Response.Write("<script language='javascript'>alert('刪除成功!');</script>");
}
catch (System.Data.SqlClient.SqlException E)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(E.Message);
}
finally
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
this.bind();
}
}