Repeater控件使用(含删除,分页功能)

摘自:http://www.cnblogs.com/alanliu/archive/2008/02/25/914779.html

前臺代碼

<%@ 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" 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; 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();
}
}

Repeater控件使用(含删除,分页功能)的更多相关文章

  1. SqlServer分页存储过程(多表查询,多条件排序),Repeater控件呈现数据以及分页

        存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出 ...

  2. Android实现图片滚动控件,含页签功能,让你的应用像淘宝一样炫起来

    首先题外话,今天早上起床的时候,手滑一下把我的手机甩了出去,结果陪伴我两年半的摩托罗拉里程碑一代就这么安息了,于是我今天决定怒更一记,纪念我死去的爱机. 如果你是网购达人,你的手机上一定少不了淘宝客户 ...

  3. repeater控件自定义Url分页带参数

    repeater控件的效果图如下: 该页面实现的功能如下: 1.上下分页,(也可以带首页和末页,我只是禁掉了没用) 2.根据用户输入的指定分页索引进行跳转 3.根据筛选数据的参数进行URL分页的参数传 ...

  4. asp.net动态网站repeater控件使用及分页操作介绍

    asp.net动态网站repeater控件使用及分页操作介绍 1.简单介绍 Repeater 控件是一个容器控件,可用于从网页的任何可用数据中创建自定义列表.Repeater 控件没有自己内置的呈现功 ...

  5. Repeater控件的分页实现

    本文讲解Repeater控件与PagedDataSource相结合实现其分页功能.PagedDataSource 类封装那些允许数据源控件(如 DataGrid.GridView)执行分页操作的属性. ...

  6. 使用Repeater控件实现三层嵌套以及分页效果

    PS: 第一次用Repeater控件 记录一下 请忽略我的命名不规范  请忽略我的最终效果图(太丑了) 需要用到的朋友可以自行调整的漂亮点 ====================最终效果图===== ...

  7. repeater控件实现分页

    repeater控件实现排序的方法,今天我再向大家介绍repeater控件如何实现分页的效果. 分页分为真分页和假分页. 真分页:控件上一页需要显示多少数据,就从数据库取出并绑定多少数据,每次换页时都 ...

  8. ASP.NET Repeater控件实现简单分页

    早上,有看MSDN,看到了 PagedDataSource 类 http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.pa ...

  9. 使用Sql分页方法给Repeater控件分页的方法

    页面代码 <div class="bookList"> <asp:Repeater ID="rpBooks" runat="serv ...

随机推荐

  1. 中国的 Android:尚未发掘的应用市场?

    作者: Wendy Boswell 本周,中国的搜索引擎公司百度最新公布的一篇报告介绍了中国 Android 用户的移动趋势. 下面是一些有价值的统计数据: 眼下在中国,每天的 Android 活跃用 ...

  2. MySQL、PostgreSQL、Ingres r3、MaxDB等开源数据库的详细比较

    1.MySQL 5 作为当今最流行的开放源码数据库之一,MySQL数据库为用户提供了一个相对简单的 解决方案,适用于广泛的应用程序部署,能够降低用户的TCO.MySQL是一个多线程.结构化查询语言(S ...

  3. wwdc2016-session707 Notifications(draft)

    Introduction to Notificationshttps://developer.apple.com/wwdc2016/707 通知这哥们说话有点不清晰啊. 远程通知本地通知 可以被操作的 ...

  4. mysql查询结果添加序列号

    第一种方法: select   (@i:=@i+1)   as   i,table_name.*   from   table_name,(select   @i:=0)   as   it 第二种方 ...

  5. 谈"自驱力"

    最新说明: 1.标题是为了博眼球取的,请不大家不要纠结具体薪资数字,我瞎取的 2.请注意素质,不要满口喷粪,不要搞人身攻击,尊重别人,就是尊重你自己 3.请大家就事论事,不要胡乱臆想,请站在全局的角度 ...

  6. saiku 元数据存储分析

    一.介绍 使用saiku的人一定对他的元数据存储都特别感兴趣,特别是有分布式管理需求的项目,更是迫切需要了解.其实它是使用Apache的开源项目Jackrabbit管理文件的! 二.代码跟踪 我也是使 ...

  7. [转]Python格式化输出

    今天写程序又记不清格式化输出细节了……= =索性整理一下. python print格式化输出. 1. 打印字符串 print ("His name is %s"%("A ...

  8. java 实现验证码

    package edu.zzuli.common; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; i ...

  9. 使用go的ssh包快速打造一个本地命令行ssh客户端

    热身运动

  10. Google Chrome 扩展程序开发

    根据公司的规定,每月八小时,弹性工作制.所以大家平时来的不太准时,如果有事,下班也就早些回去了.所以一个月下来工作时间可能不够,但是公司的考勤日历是这样的: 除了请假和法定节假日外,其他样式显示都是一 ...