Repeater的使用
1.页面代码
如果要分页,那么页面开头必须写(<%@ Register Src="~/Controls/Page.ascx" TagName="Page" TagPrefix="uc1" %>)
并且分页,页脚<uc1:Page ID="Page2" runat="server" /> 前面的uc1要跟上面的TagPrefix值一样
<table class="table" id="gv">
<%--头标--%>
<thead>
<tr>
<td width="50px" class="auto-style1">
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="DeleteByChk" OnClientClick="javascript:return checkValues('您确定要批量删除数据吗?')">删除</asp:LinkButton>
<input type="checkbox" name="ckb" class="checkall"/>
</td>
<td width="50px" class="auto-style1"><span style="margin-left:20px;">序</span></td>
<td width="100px" class="auto-style1">制单日期</td>
<td width="50px" class="auto-style1">订单状态</td>
<td width="250px" class="auto-style1">任务名称</td>
<td width="50px" class="auto-style1">销售编号</td>
<td width="50px" class="auto-style1">合同编号</td>
<td width="50px" class="auto-style1">客户名称</td>
<td width="50px" class="auto-style1">联系人</td>
<td width="50px" class="auto-style1">联系电话</td>
<td width="50px" class="auto-style1">管理</td>
</tr>
</thead>
<%--数据的绑定--%>
<asp:Repeater runat="server" ID="rpt">
<ItemTemplate>
<tr>
<td><input runat="server" id="chk" type="checkbox" value='<%#Eval("SId")%>' class="checkdelete"/></td>
<td><span style="margin-left:20px;"><%# Container.ItemIndex+1 %></span></td>
<td><span style="margin-left:25px;"><%#Eval("SOperDate")%></span></td>
<td><span style="margin-left:25px;"><%#Eval("SIsLock")%></span></td>
<td><span style="margin-left:25px;"><%#Eval("SName")%></span></td>
<td><span style="margin-left:25px;"><%#Eval("SCode")%></span></td>
<td><span style="margin-left:25px;"><%#Eval("SConNo")%></span></td>
<td><span style="margin-left:25px;"><%#Eval("SComId")%></span></td>
<td><span style="margin-left:25px;"><%#Eval("SLinkMan")%></span></td>
<td><span style="margin-left:25px;"><%#Eval("STell")%></span></td>
<td class="manage">
<a href="TaskInterManage.aspx?SId=<%#Eval("SId") %>" class="show">编辑</a>
<asp:LinkButton runat="server" ID="lb_del" class="delete" title="你确定要删除这一项吗?" OnClick="Delete" >删除</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<%--分页,页脚--%>
<table class="table">
<tr>
<td class="page">
<span style="float:left;" id="num" runat="server"></span>
<uc1:Page ID="Page2" runat="server" /> </td>
</tr>
</table>
2.数据的展示
private void show() { DataTable dt = System_Project_TasksBLL.GetList(""); //分页 int pageNumber = 1;//页数 int pageSize = 50;//每一页显示数 //判断是否需要分页 if (!string.IsNullOrEmpty(Request.QueryString["page"])) pageNumber = Convert.ToInt32(Request.QueryString["page"]);
//把datatable类型的数据转换为list集合类型的数据 List<System_Project_Tasks> list = new List<System_Project_Tasks>(); foreach (DataRow item in dt.Rows) { System_Project_Tasks data = new System_Project_Tasks(); data.SId = Convert.ToInt32(item["SId"].ToString()); data.SOperDate = Convert.ToDateTime(item["SOperDate"].ToString()); data.SIsLock = int.Parse(item["SIsLock"].ToString()); data.SName = item["SName"].ToString(); data.SCode = item["SCode"].ToString(); data.SConNo = item["SConNo"].ToString(); data.SComId = item["SComId"].ToString(); data.SLinkMan = item["SLinkMan"].ToString(); data.STell = item["STell"].ToString(); list.Add(data); }
//筛选要显示的数据 PagedDataSource pageDataSource = new PagedDataSource() { DataSource = list,//数据源 AllowPaging = true,//是否开启分页 PageSize = pageSize,//每一页显示数 CurrentPageIndex = pageNumber,//开始页的位置 }; //下脚的分页菜单的制作,pageNumber:当前页面的页数 pageDataSource.PageCount:获取数据一共有多少页 this.Page2.sty("meneame", pageNumber, pageDataSource.PageCount, "?page="); //赋值 this.num.InnerHtml = string.Concat("当前总计 - <span style='color:#ff0000; font-weight:bold;'>",dt.Rows.Count , "</span>条-数据"); this.rpt.DataSource = pageDataSource; this.rpt.DataBind(); }
3.对控件的一些基本操作
protected void Delete(object sender, EventArgs e) { //查找此控件的上一个层级 RepeaterItem parent = (sender as LinkButton).Parent as RepeaterItem; //在此层级下面查找控件(并不是找此层级的子集) HtmlInputCheckBox htmlInputCheckBox = parent.FindControl("chk") as HtmlInputCheckBox; //获取chekbox的value值(id) int num = Convert.ToInt32(htmlInputCheckBox.Value); //删除 if (bll.Delete(num)) { string str = HttpContext.Current.Server.HtmlEncode("您好!工程测试单删除成功!"); Response.Redirect(string.Concat("/InfoTip/Operate_Success.aspx?returnpage=", base.Request.Url.AbsoluteUri, "&tip=", str)); } } protected void DeleteByChk(object sender, EventArgs e) { //遍历Repeater每一行数据 foreach (RepeaterItem item in this.rpt.Items) { //获取每一行数据中的id叫chk的控件 HtmlInputCheckBox htmlInputCheckBox = item.FindControl("chk") as HtmlInputCheckBox; //判断此行数据的checkbox有没有勾选上 if (!htmlInputCheckBox.Checked) { //如果没有,那么跳过此次循环 continue; } //获取id int num = Convert.ToInt32(htmlInputCheckBox.Value); //调用bll层方法删除 bll.Delete(num); } string str = HttpContext.Current.Server.HtmlEncode("您好!邮件已彻底删除!"); base.Response.Redirect(string.Concat("/InfoTip/Operate_Success.aspx?returnpage=", base.Request.Url.AbsoluteUri, "&tip=", str)); }
4.页面的展示