repeater控件使用

Repeater控件使用(含删除,分页功能)
以SQL SERVER2000自带数据库Northwind中Customers表示例.
前台aspx代以码:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater.aspx.cs" Inherits="Repeater" %>

<!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>Repeater控件使用</title>
<script language="javascript" type="text/javascript">
function Check(parentChk,ChildId)
{
var oElements = document.getElementsByTagName("INPUT");
var bIsChecked = parentChk.checked;

for(i=0; i<oElements.length;i++)
{
if( IsCheckBox(oElements[i]) &&
IsMatch(oElements[i].id, ChildId))
{
oElements[i].checked = bIsChecked;
}
}
}

function IsMatch(id, ChildId)
{
var sPattern ='^Repeater1.*'+ChildId+'$';
var oRegExp = new RegExp(sPattern);
if(oRegExp.exec(id))
return true;
else
return false;
}

function IsCheckBox(chk)
{
if(chk.type == 'checkbox') return true;
else return false;
}

</script>
</head>

<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" οnclick="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>
后台CS部分代码:

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;

public partial class Repeater : System.Web.UI.Page
{
PagedDataSource PDS = new PagedDataSource();
private string ConnStr = ConfigurationManager.AppSettings["dbConnectionString"];
private string strsql = "";

protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.bind();
}
}

private void bind()
{
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;

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;

if (TotalCount == 0)
TotalPage = 1;
else
{
if (TotalCount % PDS.PageSize == 0)
TotalPage = TotalCount / PDS.PageSize;
else
TotalPage = TotalCount / PDS.PageSize + 1;
}

PDS.CurrentPageIndex = CurPage-1;
lblCurrentPage.Text = "共" + TotalCount.ToString() + "条记录 当前页:" + CurPage.ToString() + "/" + TotalPage;

lnkFrist.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
if (!PDS.IsFirstPage)
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);

if (!PDS.IsLastPage)
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
lnkEnd.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + TotalPage;

Repeater1.DataSource=PDS;
Repeater1.DataBind();
}

protected void btnDel_Click(object sender, EventArgs e)
{
string ID = "";

for (int i = 0; i < this.Repeater1.Items.Count; i++)
{
CheckBox cbox = (CheckBox)this.Repeater1.Items[i].FindControl("chkItem");
if (cbox.Checked == true)
{
if (ID == "")
{
ID = "'"+((Label)this.Repeater1.Items[i].FindControl("lblID")).Text+"'";
}
else
{
ID += "," + "'" + ((Label)this.Repeater1.Items[i].FindControl("lblID")).Text + "'";
}
}
}
strsql = "delete from Customers where CustomerID in (" + ID + ")";
try
{
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)
{
throw new Exception(E.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
this.bind();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值