Repeater控件使用(含删除,分页功能)
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控件使用(含删除,分页功能)的更多相关文章
- SqlServer分页存储过程(多表查询,多条件排序),Repeater控件呈现数据以及分页
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出 ...
- Android实现图片滚动控件,含页签功能,让你的应用像淘宝一样炫起来
首先题外话,今天早上起床的时候,手滑一下把我的手机甩了出去,结果陪伴我两年半的摩托罗拉里程碑一代就这么安息了,于是我今天决定怒更一记,纪念我死去的爱机. 如果你是网购达人,你的手机上一定少不了淘宝客户 ...
- repeater控件自定义Url分页带参数
repeater控件的效果图如下: 该页面实现的功能如下: 1.上下分页,(也可以带首页和末页,我只是禁掉了没用) 2.根据用户输入的指定分页索引进行跳转 3.根据筛选数据的参数进行URL分页的参数传 ...
- asp.net动态网站repeater控件使用及分页操作介绍
asp.net动态网站repeater控件使用及分页操作介绍 1.简单介绍 Repeater 控件是一个容器控件,可用于从网页的任何可用数据中创建自定义列表.Repeater 控件没有自己内置的呈现功 ...
- Repeater控件的分页实现
本文讲解Repeater控件与PagedDataSource相结合实现其分页功能.PagedDataSource 类封装那些允许数据源控件(如 DataGrid.GridView)执行分页操作的属性. ...
- 使用Repeater控件实现三层嵌套以及分页效果
PS: 第一次用Repeater控件 记录一下 请忽略我的命名不规范 请忽略我的最终效果图(太丑了) 需要用到的朋友可以自行调整的漂亮点 ====================最终效果图===== ...
- repeater控件实现分页
repeater控件实现排序的方法,今天我再向大家介绍repeater控件如何实现分页的效果. 分页分为真分页和假分页. 真分页:控件上一页需要显示多少数据,就从数据库取出并绑定多少数据,每次换页时都 ...
- ASP.NET Repeater控件实现简单分页
早上,有看MSDN,看到了 PagedDataSource 类 http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.pa ...
- 使用Sql分页方法给Repeater控件分页的方法
页面代码 <div class="bookList"> <asp:Repeater ID="rpBooks" runat="serv ...
随机推荐
- XSHELL配色方案及导入配色方案的方法
[ubuntu] text(bold)=ffffff magenta(bold)=ad7fa8 text=ffffff white(bold)=eeeeec green=4e9a06 red(bold ...
- State状态设计模式
1.状态模式:改变对象的行为 一个用来改变类的(状态的)对象. 2:问题:当你自己实现 State 模式的时候就会碰到很多细节的问题,你必须根据自己的需要选择合适的实现方法, 比如用到的状态(Stat ...
- 深圳测试研讨会圆满结束,PPT共享
深圳测试研讨会圆满结束,PPT共享http://www.automationqa.com/forum.php?mod=viewthread&tid=3417&fromuid=29
- 堆的基础题目学习(EPI)
堆的应用范围也比较广泛,经常游走在各种面试题目之前,不论算法设计的题目还是海量数据处理的题目,经常能看到这种数据结构的身影.堆其实就是一个完全二叉树的结构,经常利用数组来实现.包含最大堆和最小堆两种. ...
- web通过ActiveX打印
最近做了一个activex控件,可以通过html页面动态设置报表文件的数据,控件里的报表是通过FastReport实现了,可以通过FastReport先把报表设置好.欢迎大家提意见 控件及Demo下载
- 谈谈对从业IT行业看法
做后端开发也有五年了,从工厂到IT行业转化很大,当然最后离职的工厂想也没想过会写代码为生. 是什么变动会让我走入这一行呢? 1.思想作怪 *我当时就想,我认为不应该一辈子只做这狗屎事,起码在当时看来就 ...
- nginx后的tomcat获取真实用户ip
目前大部分获取ip的方式:beat.getRequest().getRemoteAddr()但是,如果通过nginx反向代理的话,就获取不到真实ip,是获取的nginx的ip 需要:添加 pro ...
- 使用Merge Into 语句实现 Insert/Update
网址: http://www.eygle.com/digest/2009/01/merge_into_insertupdate.html 动机: 想在Oracle中用一条SQL语句直接进行Insert ...
- windbg入门
1.下载安装windbg Windows 10 调试工具 (WinDbg) 如果你仅需要 Windows 10 调试工具,而不需要 WDK 10 或 Visual Studio 2015,你可以将调试 ...
- 代码演示 .NET 4.5 自带的 ReadonlyCollection 的使用
代码如下: 1. using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...