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 ...
随机推荐
- [js] js判断浏览器(转)
(function($, window, document,undefined){ if(!window.browser){ var userAgent = navigator.userAgent.t ...
- MapReduce的一点理解
对于MapReduce编程,大概率的流程用过的人或多或少都清楚,但是归结到细节上,就有的地方不清楚了,下面根据自己的疑问,加上从网上各处,找到的被人的描述,最自己的疑问做出回答. 1. MapRedu ...
- Python: 收集所有命名参数
有时候把Python函数调用的命名参数都收集到一个dict中可以更方便地做参数检查,或者直接由参数创建attribute等.更简单的理解就是def foo(*args, **kwargs): pass ...
- 学习之路三十八:Hook(钩子)的学习
好久没写文章了,还记得年前面试了一家公司,为了检测一下我的学习能力,给了我一个任务,做一个自动登录并自动操作菜单的程序. 花了几天的时间研究了Hook以及使用WindowsAPI操作程序的知识,现在记 ...
- 算法 - 求两个自然数的最小公倍数(C++)
//************************************************************************************************** ...
- 气球或者泡泡向上飘动 jQuery插件
圣诞.元旦要来了,公司以往基本每个月至少要搞一两款手机小游戏来宣传产品,这次也不例外!! 之前做过,按压柚子.许愿.吃柚子等等小游戏,这次是做个那种 气球向上飘动,戳破气球,随机获取奖品.如下图: 手 ...
- java 流
http://www.iteye.com/magazines/132-Java-NIO http://liyuanning.blog.163.com/blog/static/4573228620101 ...
- Android开发之ProgressDialog在独立Thread线程中更新进度
简单的需求:在一个工作Thread中更新进度对话框ProgressDialog 遇到的问题: 1,创建需要Context,这个需要传进来 2,Thread中不能创建ProgressDialog,否则需 ...
- itextpdf JAVA 输出PDF文档
使用JAVA生成PDF的时候,还是有些注意事项需要处理的. 第一.中文问题,默认的itext是不支持中文的,想要支持,需要做些处理. 1.直接引用操作系统的中文字体库支持,由于此方案限制性强,又绑定了 ...
- [转]Android开发最佳实践
——欢迎转载,请注明出处 http://blog.csdn.net/asce1885 ,未经本人同意请勿用于商业用途,谢谢—— 原文链接:https://github.com/futurice/and ...