页面前台代码:
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:DataList ID="DataList1" runat="server" Width="468px" OnItemDataBound="DataList1_ItemDataBound">
13 <ItemTemplate>
14 <div><%#DataBinder.Eval(Container.DataItem,"title","{0}") %> </div>
15
16 <div>
17 <%#DataBinder.Eval(Container.DataItem,"title","{0}") %></div>
18 <div align="right">
19 <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"id","{0}") %>' OnCommand="LinkButton1_Command">回复留言</asp:LinkButton>
20 <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "del.aspx?id="+Eval("id") %>'>删除留言</asp:HyperLink>
21 </div>
22 </ItemTemplate>
23 <SeparatorTemplate>
24 <hr />
25 </SeparatorTemplate>
26 </asp:DataList></div>
27 <asp:HyperLink ID="lnkPrev" runat="server">上一页</asp:HyperLink>
28 <asp:HyperLink ID="lnkNext" runat="server">下一页</asp:HyperLink>
29 </form>
30</body>
31</html>
32
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:DataList ID="DataList1" runat="server" Width="468px" OnItemDataBound="DataList1_ItemDataBound">
13 <ItemTemplate>
14 <div><%#DataBinder.Eval(Container.DataItem,"title","{0}") %> </div>
15
16 <div>
17 <%#DataBinder.Eval(Container.DataItem,"title","{0}") %></div>
18 <div align="right">
19 <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"id","{0}") %>' OnCommand="LinkButton1_Command">回复留言</asp:LinkButton>
20 <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "del.aspx?id="+Eval("id") %>'>删除留言</asp:HyperLink>
21 </div>
22 </ItemTemplate>
23 <SeparatorTemplate>
24 <hr />
25 </SeparatorTemplate>
26 </asp:DataList></div>
27 <asp:HyperLink ID="lnkPrev" runat="server">上一页</asp:HyperLink>
28 <asp:HyperLink ID="lnkNext" runat="server">下一页</asp:HyperLink>
29 </form>
30</body>
31</html>
32
后台代码:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11
12public partial class _Default : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!IsPostBack)
17 {
18 BindData();
19 }
20 }
21
22 private void BindData()
23 {
24 string connstr=ConfigurationManager.AppSettings["ConnectionString"].ToString();
25 SqlConnection con = new SqlConnection(connstr);
26 if (con.State.Equals(ConnectionState.Closed))
27 {
28 con.Open();
29 }
30
31 string sqlText = "select * from guestbook";
32 SqlDataAdapter sda = new SqlDataAdapter(sqlText,con);
33 DataSet ds = new DataSet();
34 sda.Fill(ds);
35
36 PagedDataSource ps = new PagedDataSource();
37 ps.DataSource = ds.Tables[0].DefaultView;
38 ps.AllowPaging = true;
39 ps.PageSize = 10;
40 int CurPage;
41
42 if (Request.QueryString["Page"] != null)
43 {
44 CurPage = Convert.ToInt32(Request.QueryString["Page"]);
45 }
46 else
47 {
48 CurPage = 1;
49 }
50
51 ps.CurrentPageIndex = CurPage - 1;
52
53
54 if (!ps.IsFirstPage)
55 {
56 lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToInt32(CurPage-1);
57 }
58 if (!ps.IsLastPage)
59 {
60 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToInt32(CurPage+1);
61 }
62
63 this.DataList1.DataSource = ps;
64 this.DataList1.DataBind();
65
66 }
67 protected void LinkButton1_Command(object sender, CommandEventArgs e)
68 {
69 Response.Redirect("hfliuyan.aspx?id="+e.CommandArgument);
70 }
71 protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
72 {
73 HyperLink hpl=(HyperLink)(e.Item.FindControl("HyperLink1"));
74 if (hpl != null)
75
76
77 {
78 hpl.Attributes.Add("onclick","return confirm('确定删除吗?');");
79 }
80 }
81}
82
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11
12public partial class _Default : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!IsPostBack)
17 {
18 BindData();
19 }
20 }
21
22 private void BindData()
23 {
24 string connstr=ConfigurationManager.AppSettings["ConnectionString"].ToString();
25 SqlConnection con = new SqlConnection(connstr);
26 if (con.State.Equals(ConnectionState.Closed))
27 {
28 con.Open();
29 }
30
31 string sqlText = "select * from guestbook";
32 SqlDataAdapter sda = new SqlDataAdapter(sqlText,con);
33 DataSet ds = new DataSet();
34 sda.Fill(ds);
35
36 PagedDataSource ps = new PagedDataSource();
37 ps.DataSource = ds.Tables[0].DefaultView;
38 ps.AllowPaging = true;
39 ps.PageSize = 10;
40 int CurPage;
41
42 if (Request.QueryString["Page"] != null)
43 {
44 CurPage = Convert.ToInt32(Request.QueryString["Page"]);
45 }
46 else
47 {
48 CurPage = 1;
49 }
50
51 ps.CurrentPageIndex = CurPage - 1;
52
53
54 if (!ps.IsFirstPage)
55 {
56 lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToInt32(CurPage-1);
57 }
58 if (!ps.IsLastPage)
59 {
60 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToInt32(CurPage+1);
61 }
62
63 this.DataList1.DataSource = ps;
64 this.DataList1.DataBind();
65
66 }
67 protected void LinkButton1_Command(object sender, CommandEventArgs e)
68 {
69 Response.Redirect("hfliuyan.aspx?id="+e.CommandArgument);
70 }
71 protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
72 {
73 HyperLink hpl=(HyperLink)(e.Item.FindControl("HyperLink1"));
74 if (hpl != null)
75
76
77 {
78 hpl.Attributes.Add("onclick","return confirm('确定删除吗?');");
79 }
80 }
81}
82
web.config
1<?xml version="1.0"?>
2<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
3 <appSettings>
4 <add key="ConnectionString" value="server=.;database=OfficeAutoDb;uid=sa;pwd=;"/>
5 </appSettings>
6 <system.web>
7 <compilation debug="true"/></system.web></configuration>
8
2<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
3 <appSettings>
4 <add key="ConnectionString" value="server=.;database=OfficeAutoDb;uid=sa;pwd=;"/>
5 </appSettings>
6 <system.web>
7 <compilation debug="true"/></system.web></configuration>
8
所用的数据表:
CREATE TABLE [dbo].[guestbook] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[kindId] [int] NULL ,
[title] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[body] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[datetime] [datetime] NOT NULL
) ON [PRIMARY]
[id] [int] IDENTITY (1, 1) NOT NULL ,
[kindId] [int] NULL ,
[title] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[body] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[datetime] [datetime] NOT NULL
) ON [PRIMARY]