@Aiti
2018-11-14T01:15:01.000000Z
字数 7765
阅读 233
未分类
前台代码
//repeate.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeate.aspx.cs" Inherits="repeate" %><%@ Import Namespace="System.Data" %><!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>无标题页</title></head><body><form id="form1" runat="server"><div><asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" ><HeaderTemplate><%-- 我是头模板--%><table width="500"><tr style="background-color: #ccffcc;"><td>作者</td><td>书籍</td></tr></HeaderTemplate><ItemTemplate><%--我是项模板--%><tr><td><a href='repeate.aspx?id=<%# Eval("au_id")%>'><%# Eval("au_lname") %></a></td><td><asp:Repeater ID="Repeater2" runat="server" DataSource='<%# Eval("myrela") %>'><ItemTemplate><%# Eval("[/"title_id/"]") %></ItemTemplate></asp:Repeater></td></tr></ItemTemplate><SeparatorTemplate><%--这是分隔线模板--%><tr><td colspan="2"><hr style="border-top:1pt;"/></td></tr></SeparatorTemplate><FooterTemplate><%--这是脚模板--%><tr><td colspan="2" style="font-size:12pt;color:#0099ff; background-color:#e6feda;">共<asp:Label ID="lblpc" runat="server" Text="Label"></asp:Label>页 当前为第<asp:Label ID="lblp" runat="server" Text="Label"></asp:Label>页<asp:HyperLink ID="hlfir" runat="server" Text="首页"></asp:HyperLink><asp:HyperLink ID="hlp" runat="server" Text="上一页"></asp:HyperLink><asp:HyperLink ID="hln" runat="server" Text="下一页"></asp:HyperLink><asp:HyperLink ID="hlla" runat="server" Text="尾页"></asp:HyperLink>跳至第<asp:DropDownList ID="ddlp" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlp_SelectedIndexChanged" ></asp:DropDownList>页</td></tr></table></FooterTemplate></asp:Repeater></div></form></body></html>
后台代码
using System;using System.Data;using System.Data.SqlClient;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;public partial class repeate : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){Repeater1.DataSource = pds();Repeater1.DataBind();}}private PagedDataSource pds(){PagedDataSource pds = new PagedDataSource();//string connstring = ConfigurationManager.ConnectionStrings["pconn"].ConnectionString;// SqlConnection con = new SqlConnection(connstring);// DataSet ds = new DataSet();//SqlDataAdapter sda = new SqlDataAdapter("select * from authors",con);// sda.Fill(ds,"name");// SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor",con);// sda2.Fill(ds,"title");// ds.Relations.Add("myrela",ds.Tables["name"].Columns["au_id"],ds.Tables["title"].Columns["au_id"]);// pds.DataSource = ds.Tables["name"].DefaultView;//Comments类是Diary类的一对多的关联关系Diary Diary = session.FindObject<Diary>(CriteriaOperator.Parse("Oid=?", 1));pds.DataSource = Diary.Comments;pds.AllowPaging = true;//允许分页pds.PageSize = 5;//单页显示项数pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);return pds;}protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e){if (e.Item.ItemType == ListItemType.Footer){DropDownList ddlp = (DropDownList)e.Item.FindControl("ddlp");HyperLink lpfirst = (HyperLink)e.Item.FindControl("hlfir");HyperLink lpprev = (HyperLink)e.Item.FindControl("hlp");HyperLink lpnext = (HyperLink)e.Item.FindControl("hln");HyperLink lplast = (HyperLink)e.Item.FindControl("hlla");pds().CurrentPageIndex = ddlp.SelectedIndex;int n = Convert.ToInt32(pds().PageCount);//n为分页数int i = Convert.ToInt32(pds().CurrentPageIndex);//i为当前页Label lblpc = (Label)e.Item.FindControl("lblpc");lblpc.Text = n.ToString();Label lblp = (Label)e.Item.FindControl("lblp");lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);if (!IsPostBack){for (int j = 0; j < n; j++){ddlp.Items.Add(Convert.ToString(j + 1));}}if (i <= 0){lpfirst.Enabled = false;lpprev.Enabled = false;lplast.Enabled = true;lpnext.Enabled = true;}else{lpprev.NavigateUrl = "?page=" + (i - 1);}if (i >= n - 1){lpfirst.Enabled = true;lplast.Enabled = false;lpnext.Enabled = false;lpprev.Enabled = true;}else{lpnext.NavigateUrl = "?page=" + (i + 1);}lpfirst.NavigateUrl = "?page=0";//向本页传递参数pagelplast.NavigateUrl = "?page=" + (n - 1);ddlp.SelectedIndex = Convert.ToInt32(pds().CurrentPageIndex);//更新下拉列表框中的当前选中页序号}}protected void ddlp_SelectedIndexChanged(object sender, EventArgs e){//脚模板中的下拉列表框更改时激发string pg=Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue)-1));//获取列表框当前选中项Response.Redirect("repeate.aspx?page="+pg);//页面转向}}
其中发现的问题:
因为DataSource的数据类型是List()数据集; 所以
var data = new XPQuery<Diary>(session);pds.DataSource = data;
会报错:“无法计算未实现 ICollection 的数据源中的计数”;所以有两种改法
1、
var data = new XPQuery<Diary>(session).ToList();pds.DataSource = data;
2、
var data = new XPCollection<Diary>(session);pds.DataSource = data;
其中XPQuery也可实现查询前几条数据;如前6条数据:
var data = new XPQuery<Diary>(session).Take(6).ToList();pds.DataSource = data;
第一种
<style>.wrap-dialog {position: fixed;top: 0;left: 0;width: 100%;height: 100%;font-size: 16px;text-align: center;background-color: rgba(0, 0, 0, .4);z-index: 99999;}.dialog1 {position: relative;margin: 15% auto;width: 300px;background-color: #FFFFFF;}.dialog1 .dialog-header {height: 20px;padding: 10px;background-color: lightskyblue;}.dialog1 .dialog-body {height: 30px;padding: 20px;}.dialog1 .dialog-footer {padding: 8px;background-color: whitesmoke;}.btn {width: 70px;padding: 2px;}.hide {display: none;}.ml50 {margin-left: 50px;}</style>--------------------<script>function dialogBox(message, yesCallback, noCallback) {if (message) {$('.dialog-message').html(message);}// 显示遮罩和对话框$('.wrap-dialog').removeClass("hide");// 确定按钮$('#confirm').click(function () {$('.wrap-dialog').addClass("hide");yesCallback();});// 取消按钮$('#cancel').click(function () {$('.wrap-dialog').addClass("hide");noCallback();});}$(function () {$(".delbtn").click(function () {var that=this;dialogBox("你确认删除此条信息??",function () {var h=$(that).parent().find("div").find("a").attr("href");h=h.substring(h.indexOf('rpt'),h.lastIndexOf('e')+1);__doPostBack(h,'');},function(){});});});</script>--------------------<div class="wrap-dialog hide"><div class="dialog1"><div class="dialog-header"><span class="dialog-title">删除确认</span></div><div class="dialog-body"><span class="dialog-message">你确认删除此条信息?</span></div><div class="dialog-footer"><input type="button" class="btn" id="confirm" value="确认" /><input type="button" class="btn ml50" id="cancel" value="取消" /></div></div></div><asp:Repeater runat="server" ID="rpt" ClientIDMode="Static"><ItemTemplate><tr><td><a href='ViewAll.aspx?rid=<%#Eval("Oid") %>'><%#Eval("RecordName") %> (合计:<%#Eval("Grade") %> )</a> </td><td><a href="javascript:void(0)" class='del delbtn'>删除</a><div style="display:none;"><asp:LinkButton ID="btnDelete" runat="server" cssClass='del' CommandArgument='<%#Eval("Oid") %>'OnClick="Del">删除</asp:LinkButton></div></td></tr></ItemTemplate></asp:Repeater>----------后台代码:public void Del(object sender, EventArgs e){int id = Convert.ToInt32((sender as LinkButton).CommandArgument);var r = new XPCollection<CalculationReport>(session, CriteriaOperator.Parse("Oid=?", id));session.Delete(r);session.Save(r);rpt.DataSource = new XPCollection<CalculationReport>(session, CriteriaOperator.Parse("User.LoginName=?", currentUser.LoginName), new SortProperty("RecordName", DevExpress.Xpo.DB.SortingDirection.Ascending));rpt.DataBind();}
第二种
<asp:Repeater runat="server" ID="rpt" ClientIDMode="Static"><ItemTemplate><tr><td><i class="fa fa-file" style="color:#d4cbd4;margin-right:10px;"></i></td><td><a style="font-size:14px;color:#2599e3;" href='ViewAll.aspx?rid=<%#Eval("Oid") %>'> <%#Eval("RecordName") %> (合计:<%#Eval("Grade") %> )</a> </td><td><%--<asp:LinkButton ID="btnDelete" runat="server" cssClass='del' Text="删除" CommandName="delete" CommandArgument='<%#Eval("Oid") %>'>删除</asp:LinkButton>--%><asp:LinkButton ID="btnDelete" runat="server" cssClass='del' CommandArgument='<%#Eval("Oid") %>' OnClientClick="return confirm('是否删除')" OnClick="Del" >删除</asp:LinkButton></td></tr></ItemTemplate></asp:Repeater>后台代码与上面一致