DataList 使用的方法
1。这个是MSDN上面的,没有取库中的数据绑定,而是自己申明了一个 DataTable 进行绑定的
前台代码:
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dataList1.aspx.cs" Inherits="dataList1" %>
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>DataList 测试</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div style="text-align: center">
12 <asp:DataList ID="ItemDataList" runat="server" RepeatColumns="3" Width="288px">
13 <ItemTemplate>
14 描述:<%#Eval("string") %>><br />
15 价格:<%#Eval("double","{0:c}") %>><br />
16 <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("image") %>'/>
17 </ItemTemplate>
18 </asp:DataList> </div>
19 </form>
20</body>
21</html>
22
后台代码:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class dataList1 : System.Web.UI.Page
13{
14 //创建一个DataList 的数据源
15 ICollection CreateDataSource()
16 {
17 //申明一个数据表和一个数据行;
18 DataTable dt = new DataTable();
19 DataRow dr;
20
21 //定义表列; 包括 表名称和表的数据类型;
22 dt.Columns.Add("int",typeof(Int32));
23 dt.Columns.Add("string",typeof(string));
24 dt.Columns.Add("double", typeof(double));
25 dt.Columns.Add("image", typeof(string));
26
27 for (int i = 0; i < 9;i++ )
28 {
29 dr = dt.NewRow();
30
31 dr[0] = i;
32 dr[1] = "标题:" + i;
33 dr[2] = 1.21*(i+1);
34 dr[3] = "image/" + i + ".jpg";
35 dt.Rows.Add(dr);
36 }
37 DataView dv = new DataView(dt);
38 return dv;
39 }
40
41
42 protected void Page_Load(object sender, EventArgs e)
43 {
44 this.ItemDataList.DataSource = CreateDataSource();
45 this.ItemDataList.DataBind();
46 }
47}
48
2。这个是thcjp.cnblogs.com 的一个留言本,学习了一下,记下学习代码
前台:book.aspx
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="book.aspx.cs" Inherits="book" %>
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 <style type="text/css">
9 td{
10 font-size:13px;
11 }
12 </style>
13</head>
14<body>
15 <form id="form1" runat="server">
16 <div style="text-align: center">
17 <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound" Width="120px">
18 <ItemTemplate>
19 <table border="1" style="width: 614px">
20 <tr>
21 <td style="width: 153px; background-color: #ccff66; height: 27px;">
22 <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label></td>
23 <td style="width: 369px; text-align: left; height: 27px; background-color: #ccccff;">
24 <asp:Label ID="Label2" runat="server" Text='<%# Server.HtmlDecode(Eval("title").ToString()) %>'></asp:Label></td>
25 <td style="width: 100px; height: 27px; background-color: #cccc66;">
26 <a href='huifu.aspx?id=<%# Eval("id")%>'><asp:Image ID="Image1" runat="server" ImageUrl="~/images/quote.gif" ToolTip='回复' /></a></td>
27 </tr>
28 <tr>
29 <td style="width: 153px; height: 84px;" valign="top">
30 <br />
31 <asp:Image ID="Image2" runat="server" ImageUrl='<%# Eval("face") %>' /><br />
32 <br />
33 <asp:Image ID="Image7" runat="server" ImageUrl="~/images/icon_qq.gif" ToolTip='<%# Eval("qq") %>' />
34 <asp:Image ID="Image3" runat="server" ImageUrl="~/images/icon_mail.gif" ToolTip='<%# Eval("email") %>' />
35 <asp:Image ID="Image4" runat="server" ImageUrl="~/images/icon_msn.gif" ToolTip='<%# Eval("msn") %>' />
36 <asp:Image ID="Image5" runat="server" ImageUrl="~/images/icon_homepage.gif" ToolTip='<%# Eval("url") %>' />
37 <asp:Image ID="Image6" runat="server" ImageUrl="~/images/icon_ip.gif" ToolTip='<%# Eval("ip") %>' />
38 <br />
39 <br />
40 <asp:Label ID="Label4" runat="server" Text='<%# Eval("dtt") %>'></asp:Label>
41 </td>
42 <td style="text-align: left; height: 84px;" colspan="2" valign="top">
43 <br />
44 <asp:Label ID="Label3" runat="server" Text='<%# Server.HtmlDecode(Eval("concent").ToString()) %>'></asp:Label><br />
45 <asp:Panel ID="dl" runat="server" Height="50px" Visible="False" Width="350px">
46 请输入密码<br />
47 <asp:TextBox ID="dlpwd" runat="server" Width="120px"></asp:TextBox>
48 <asp:Button ID="button1" runat="server" Text="查看留言" OnClick="button1_Click" />
49 <br />
50 <asp:Label ID="iidd" runat="server" Text='<%# Eval("id") %>' Visible="False"></asp:Label><br />
51 </asp:Panel>
52 <asp:Panel ID="Panel2" runat="server" Height="50px" Visible="False" Width="350px">
53 ==============回复内容==================<br />
54 <asp:DataList ID="DataList2" runat="server">
55 <ItemTemplate>
56 <table border="1" style="width: 335px">
57 <tr>
58 <td style="width: 100px; background-color: #99cccc; height: 20px;">
59 <asp:Label ID="Label6" runat="server" Text='<%# Eval("title") %>'></asp:Label>
60 (<asp:Label ID="Label7" runat="server" Text='<%# Eval("dtt","{0:d}") %>'></asp:Label>)</td>
61 </tr>
62 <tr>
63 <td style="width: 100px; background-color: #99cccc">
64 <asp:Label ID="Label8" runat="server" Text='<%# Server.HtmlDecode(Eval("concent").ToString()) %>'></asp:Label></td>
65 </tr>
66 </table>
67 </ItemTemplate>
68 </asp:DataList></asp:Panel>
69 </td>
70 </tr>
71 </table>
72 </ItemTemplate>
73 </asp:DataList><br />
74 <table border="1" style="width: 606px; height: 63px">
75 <tr>
76 <td style="width: 50%">
77 </td>
78 <td>
79 <asp:LinkButton ID="pageup" runat="server" OnClick="pageup_Click">上一页</asp:LinkButton>
80 <asp:DropDownList ID="pageddl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="pageddl_SelectedIndexChanged">
81 </asp:DropDownList>
82 <asp:LinkButton ID="pagedown" runat="server" OnClick="pagedown_Click">下一页</asp:LinkButton>
83 <asp:Label ID="pagelbl" runat="server" Text="1" Visible="False"></asp:Label></td>
84 </tr>
85 </table>
86 <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/book.mdb"
87 SelectCommand="SELECT * FROM [guest]"></asp:AccessDataSource>
88
89 </div>
90 </form>
91</body>
92</html>
93
后台:book.aspx.cs
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class book : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 Page.Title = DB.scr("select title from config"); //标题
17 if(!Page.IsPostBack)
18 {
19 fill();
20 }
21 }
22 protected void fill()
23 {
24 int cup = Convert.ToInt32(pagelbl.Text);
25
26 PagedDataSource ps = new PagedDataSource();
27 ps.DataSource = DB.ds("select * from [guest] where vis=0 order by id desc").DefaultView;
28 //ps.DataSource = DB.ds("select * from [guest] where vis=0 order by id desc").DefaultView;
29 ps.AllowPaging = true;
30 ps.PageSize = DB.num("booknum");
31 ps.CurrentPageIndex = cup - 1; //分页索引从0开始。
32
33 if (!Page.IsPostBack)
34 {
35 for (int i = 1; i <= ps.PageCount; i++)
36 {
37 pageddl.Items.Add(i.ToString());
38 }
39 }
40 pageup.Enabled = true;
41 pagedown.Enabled = true;
42 if (ps.IsFirstPage)
43 {
44 pageup.Enabled = false;
45 }
46 if (ps.IsLastPage)
47 {
48 pagedown.Enabled = false;
49 }
50 pageddl.SelectedItem.Text = cup.ToString();
51 DataList1.DataSource = ps;
52 DataList1.DataKeyField = "id";
53 DataList1.DataBind();
54
55
56 }
57
58 protected void pageup_Click(object sender, EventArgs e)
59 {
60 //上一页
61 this.pagelbl.Text = Convert.ToString(Convert.ToInt32(this.pagelbl.Text)-1);
62 this.pageddl.SelectedValue = this.pagelbl.Text;
63 fill();
64 }
65 protected void pagedown_Click(object sender, EventArgs e)
66 {
67 //下一页
68 this.pagelbl.Text = Convert.ToString(Convert.ToInt32(this.pagelbl.Text) + 1);
69 this.pageddl.SelectedValue = this.pagelbl.Text;
70 fill();
71 }
72 protected void pageddl_SelectedIndexChanged(object sender, EventArgs e)
73 {
74 pagelbl.Text = pageddl.SelectedItem.Text;
75 fill();
76
77 }
78 protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
79 {
80
81 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
82 {
83 //获取ID号;
84 int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
85 Image qq = (Image)e.Item.FindControl("Image7");
86 qq.Visible = DB.img("select qq from [guest] where id=" + id + "");
87
88 Image email = (Image)e.Item.FindControl("Image3");
89 email.Visible = DB.img("select email from [guest] where id=" + id + "");
90
91 Image msn = (Image)e.Item.FindControl("Image4");
92 msn.Visible = DB.img("select qq from [guest] where id=" + id + "");
93
94 Image url = (Image)e.Item.FindControl("Image5");
95 url.Visible = DB.img("select url from [guest] where id=" + id + "");
96
97 Image ip = (Image)e.Item.FindControl("Image6");
98 ip.Visible = DB.img("select ip from [guest] where id=" + id + "");
99
100 Label content = (Label)e.Item.FindControl("Label3");
101 Panel dengl = (Panel)e.Item.FindControl("dl");
102
103 //if (DB.img("select pwd from [guest] where id=" + id + ""))
104 //{
105 // //如果有密码,我这里和他做的不一样,前面少了一个 !
106 // //如果要输入密码,则将内容隐藏,出现登录页面
107 // if(Session["fk"]!=null || Session["bk"]!=null)
108 // {
109 // //已经登录情况下,显示内容
110 // //将内容标签赋值。
111 // content.Text = Server.HtmlDecode(DB.scr("select concent from [guest] where id= " + id + ""));
112 // //如果有回复内容,则显示回复信息。
113 // if (Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id + "")) > 0)
114 // {
115 // Panel re = (Panel)e.Item.FindControl("Panel2");
116 // re.Visible = true;
117 // DataList read = (DataList)e.Item.FindControl("DataList2");
118 // read.DataSource = DB.scr("select * from [read] where lid=" + id + "");
119 // read.DataBind();
120
121 // }
122 // }
123 // else
124 // {//需要密码,但是还没登录
125 // dl.Visible=true;
126 // content.Visible=false;
127 // }
128 //}
129 //else
130 //{//无须密码
131
132 // content.Text = Server.HtmlDecode(DB.scr("select concent from [guest] where id= " + id + ""));
133 // //如果有回复内容,则显示回复信息。
134 // if (Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id + "")) > 0)
135 // {
136 // Panel re = (Panel)e.Item.FindControl("Panel2");
137 // re.Visible = true;
138 // DataList read = (DataList)e.Item.FindControl("DataList2");
139 // read.DataSource = DB.scr("select * from [read] where lid=" + id + "");
140 // read.DataBind();
141
142 // }
143
144 //}
145
146 if (!DB.img("select pwd from guest where id=" + id + ""))
147 {//如果没密码的话就直接显示内容以及需要的回复
148 content.Text = Server.HtmlDecode(DB.scr("select concent from guest where id=" + id + "")).ToString();
149 if (Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id + "")) > 0)
150 {//如果有回复的话就把回复panel以及下面的东西都找出来,然后绑上数据,显示出来
151 Panel re = (Panel)e.Item.FindControl("Panel2");
152 re.Visible = true;
153
154 DataList read = (DataList)e.Item.FindControl("DataList2");
155 read.DataSource = DB.ds("select * from [read] where lid=" + id + "");
156 read.DataBind();
157 }
158 }
159 else
160 {//如果有密码就把内容隐去,出现登陆项
161 if (Session["fk"] != null || Session["qx"] != null)
162 {
163 content.Text = Server.HtmlDecode(DB.scr("select concent from guest where id=" + id + "")).ToString();
164 if (Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id + "")) > 0)
165 {//如果有回复的话就把回复panel以及下面的东西都找出来,然后绑上数据,显示出来
166 Panel re = (Panel)e.Item.FindControl("Panel2");
167 re.Visible = true;
168
//下面几行是镶嵌在第一个DataList中的DataList,其实它的绑定真的很简单
169 DataList read = (DataList)e.Item.FindControl("DataList2");
170 read.DataSource = DB.ds("select * from [read] where lid=" + id + "");
171 read.DataBind();
172 }
173 }
174 else
175 {
176 dengl.Visible = true;
177 content.Visible = false;
178 }
179 }
180
181 }
182 }
183 protected void button1_Click(object sender, EventArgs e)
184 {
185 /**/////密码登录查看留言
186 //for (int i = 0; i < DataList1.Items.Count;i++ )
187 //{
188 // TextBox pwd = (TextBox)DataList1.Items[i].FindControl("dlpwd");
189 // Label id = (Label)DataList1.Items[i].FindControl("iidd");
190 // if((DB.scr("select pwd from [guest] where id=" + id.Text)).ToString() == pwd.Text.ToString())
191 // {//登录成功
192 // Session["fk"] = pwd.Text.ToString();
193 // Label content = (Label)DataList1.Items[i].FindControl("Label3");
194 // Panel dl = (Panel)DataList1.Items[i].FindControl("dl");
195
196 // content.Text = Server.HtmlDecode(DB.scr("select concent from [guest] where id=" + id.Text));
197 // content.Visible = true;
198 // dl.Visible = false;
199
200 // //如果有回复内容,则显示回复信息。
201 // if(Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id))>0)
202 // {
203 // Panel re = (Panel)DataList1.Items[i].FindControl("Panel2");
204 // re.Visible = true;
205 // DataList read = (DataList)DataList1.Items[i].FindControl("DataList2");
206 // read.DataSource=DB.scr("select * from [read] where lid=" + id);
207 // read.DataBind();
208 // }
209
210 // }
211 //}
212
213 for (int i = 0; i < DataList1.Items.Count; i++)
214 {
215 TextBox pwd = (TextBox)DataList1.Items[i].FindControl("dlpwd");
216 Label id = (Label)DataList1.Items[i].FindControl("iidd");
217 if (DB.scr("select [pwd] from [guest] where id=" + id.Text + "").ToString() == pwd.Text.ToString())
218 {
219 Session["fk"] = pwd.Text.ToString();
220
221 Label concent = (Label)DataList1.Items[i].FindControl("Label4");
222 Panel dengl = (Panel)DataList1.Items[i].FindControl("dl");
223
224 dengl.Visible = false;
225 concent.Visible = true;
226 if (Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id.Text + "")) > 0)
227 {//如果有回复的话就把回复panel以及下面的东西都找出来,然后绑上数据,显示出来
228 Panel re = (Panel)DataList1.Items[i].FindControl("Panel2");
229 re.Visible = true;
230
231 DataList read = (DataList)DataList1.Items[i].FindControl("DataList2");
232 read.DataSource = DB.ds("select * from [read] where lid=" + id.Text + "");
233 read.DataBind();
234 }
235 }
236 }
237 }
238}
239
1。这个是MSDN上面的,没有取库中的数据绑定,而是自己申明了一个 DataTable 进行绑定的
前台代码:
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dataList1.aspx.cs" Inherits="dataList1" %>
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>DataList 测试</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div style="text-align: center">
12 <asp:DataList ID="ItemDataList" runat="server" RepeatColumns="3" Width="288px">
13 <ItemTemplate>
14 描述:<%#Eval("string") %>><br />
15 价格:<%#Eval("double","{0:c}") %>><br />
16 <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("image") %>'/>
17 </ItemTemplate>
18 </asp:DataList> </div>
19 </form>
20</body>
21</html>
22
后台代码:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class dataList1 : System.Web.UI.Page
13{
14 //创建一个DataList 的数据源
15 ICollection CreateDataSource()
16 {
17 //申明一个数据表和一个数据行;
18 DataTable dt = new DataTable();
19 DataRow dr;
20
21 //定义表列; 包括 表名称和表的数据类型;
22 dt.Columns.Add("int",typeof(Int32));
23 dt.Columns.Add("string",typeof(string));
24 dt.Columns.Add("double", typeof(double));
25 dt.Columns.Add("image", typeof(string));
26
27 for (int i = 0; i < 9;i++ )
28 {
29 dr = dt.NewRow();
30
31 dr[0] = i;
32 dr[1] = "标题:" + i;
33 dr[2] = 1.21*(i+1);
34 dr[3] = "image/" + i + ".jpg";
35 dt.Rows.Add(dr);
36 }
37 DataView dv = new DataView(dt);
38 return dv;
39 }
40
41
42 protected void Page_Load(object sender, EventArgs e)
43 {
44 this.ItemDataList.DataSource = CreateDataSource();
45 this.ItemDataList.DataBind();
46 }
47}
48
2。这个是thcjp.cnblogs.com 的一个留言本,学习了一下,记下学习代码
前台:book.aspx
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="book.aspx.cs" Inherits="book" %>
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 <style type="text/css">
9 td{
10 font-size:13px;
11 }
12 </style>
13</head>
14<body>
15 <form id="form1" runat="server">
16 <div style="text-align: center">
17 <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound" Width="120px">
18 <ItemTemplate>
19 <table border="1" style="width: 614px">
20 <tr>
21 <td style="width: 153px; background-color: #ccff66; height: 27px;">
22 <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label></td>
23 <td style="width: 369px; text-align: left; height: 27px; background-color: #ccccff;">
24 <asp:Label ID="Label2" runat="server" Text='<%# Server.HtmlDecode(Eval("title").ToString()) %>'></asp:Label></td>
25 <td style="width: 100px; height: 27px; background-color: #cccc66;">
26 <a href='huifu.aspx?id=<%# Eval("id")%>'><asp:Image ID="Image1" runat="server" ImageUrl="~/images/quote.gif" ToolTip='回复' /></a></td>
27 </tr>
28 <tr>
29 <td style="width: 153px; height: 84px;" valign="top">
30 <br />
31 <asp:Image ID="Image2" runat="server" ImageUrl='<%# Eval("face") %>' /><br />
32 <br />
33 <asp:Image ID="Image7" runat="server" ImageUrl="~/images/icon_qq.gif" ToolTip='<%# Eval("qq") %>' />
34 <asp:Image ID="Image3" runat="server" ImageUrl="~/images/icon_mail.gif" ToolTip='<%# Eval("email") %>' />
35 <asp:Image ID="Image4" runat="server" ImageUrl="~/images/icon_msn.gif" ToolTip='<%# Eval("msn") %>' />
36 <asp:Image ID="Image5" runat="server" ImageUrl="~/images/icon_homepage.gif" ToolTip='<%# Eval("url") %>' />
37 <asp:Image ID="Image6" runat="server" ImageUrl="~/images/icon_ip.gif" ToolTip='<%# Eval("ip") %>' />
38 <br />
39 <br />
40 <asp:Label ID="Label4" runat="server" Text='<%# Eval("dtt") %>'></asp:Label>
41 </td>
42 <td style="text-align: left; height: 84px;" colspan="2" valign="top">
43 <br />
44 <asp:Label ID="Label3" runat="server" Text='<%# Server.HtmlDecode(Eval("concent").ToString()) %>'></asp:Label><br />
45 <asp:Panel ID="dl" runat="server" Height="50px" Visible="False" Width="350px">
46 请输入密码<br />
47 <asp:TextBox ID="dlpwd" runat="server" Width="120px"></asp:TextBox>
48 <asp:Button ID="button1" runat="server" Text="查看留言" OnClick="button1_Click" />
49 <br />
50 <asp:Label ID="iidd" runat="server" Text='<%# Eval("id") %>' Visible="False"></asp:Label><br />
51 </asp:Panel>
52 <asp:Panel ID="Panel2" runat="server" Height="50px" Visible="False" Width="350px">
53 ==============回复内容==================<br />
54 <asp:DataList ID="DataList2" runat="server">
55 <ItemTemplate>
56 <table border="1" style="width: 335px">
57 <tr>
58 <td style="width: 100px; background-color: #99cccc; height: 20px;">
59 <asp:Label ID="Label6" runat="server" Text='<%# Eval("title") %>'></asp:Label>
60 (<asp:Label ID="Label7" runat="server" Text='<%# Eval("dtt","{0:d}") %>'></asp:Label>)</td>
61 </tr>
62 <tr>
63 <td style="width: 100px; background-color: #99cccc">
64 <asp:Label ID="Label8" runat="server" Text='<%# Server.HtmlDecode(Eval("concent").ToString()) %>'></asp:Label></td>
65 </tr>
66 </table>
67 </ItemTemplate>
68 </asp:DataList></asp:Panel>
69 </td>
70 </tr>
71 </table>
72 </ItemTemplate>
73 </asp:DataList><br />
74 <table border="1" style="width: 606px; height: 63px">
75 <tr>
76 <td style="width: 50%">
77 </td>
78 <td>
79 <asp:LinkButton ID="pageup" runat="server" OnClick="pageup_Click">上一页</asp:LinkButton>
80 <asp:DropDownList ID="pageddl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="pageddl_SelectedIndexChanged">
81 </asp:DropDownList>
82 <asp:LinkButton ID="pagedown" runat="server" OnClick="pagedown_Click">下一页</asp:LinkButton>
83 <asp:Label ID="pagelbl" runat="server" Text="1" Visible="False"></asp:Label></td>
84 </tr>
85 </table>
86 <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/book.mdb"
87 SelectCommand="SELECT * FROM [guest]"></asp:AccessDataSource>
88
89 </div>
90 </form>
91</body>
92</html>
93
后台:book.aspx.cs
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class book : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 Page.Title = DB.scr("select title from config"); //标题
17 if(!Page.IsPostBack)
18 {
19 fill();
20 }
21 }
22 protected void fill()
23 {
24 int cup = Convert.ToInt32(pagelbl.Text);
25
26 PagedDataSource ps = new PagedDataSource();
27 ps.DataSource = DB.ds("select * from [guest] where vis=0 order by id desc").DefaultView;
28 //ps.DataSource = DB.ds("select * from [guest] where vis=0 order by id desc").DefaultView;
29 ps.AllowPaging = true;
30 ps.PageSize = DB.num("booknum");
31 ps.CurrentPageIndex = cup - 1; //分页索引从0开始。
32
33 if (!Page.IsPostBack)
34 {
35 for (int i = 1; i <= ps.PageCount; i++)
36 {
37 pageddl.Items.Add(i.ToString());
38 }
39 }
40 pageup.Enabled = true;
41 pagedown.Enabled = true;
42 if (ps.IsFirstPage)
43 {
44 pageup.Enabled = false;
45 }
46 if (ps.IsLastPage)
47 {
48 pagedown.Enabled = false;
49 }
50 pageddl.SelectedItem.Text = cup.ToString();
51 DataList1.DataSource = ps;
52 DataList1.DataKeyField = "id";
53 DataList1.DataBind();
54
55
56 }
57
58 protected void pageup_Click(object sender, EventArgs e)
59 {
60 //上一页
61 this.pagelbl.Text = Convert.ToString(Convert.ToInt32(this.pagelbl.Text)-1);
62 this.pageddl.SelectedValue = this.pagelbl.Text;
63 fill();
64 }
65 protected void pagedown_Click(object sender, EventArgs e)
66 {
67 //下一页
68 this.pagelbl.Text = Convert.ToString(Convert.ToInt32(this.pagelbl.Text) + 1);
69 this.pageddl.SelectedValue = this.pagelbl.Text;
70 fill();
71 }
72 protected void pageddl_SelectedIndexChanged(object sender, EventArgs e)
73 {
74 pagelbl.Text = pageddl.SelectedItem.Text;
75 fill();
76
77 }
78 protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
79 {
80
81 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
82 {
83 //获取ID号;
84 int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
85 Image qq = (Image)e.Item.FindControl("Image7");
86 qq.Visible = DB.img("select qq from [guest] where id=" + id + "");
87
88 Image email = (Image)e.Item.FindControl("Image3");
89 email.Visible = DB.img("select email from [guest] where id=" + id + "");
90
91 Image msn = (Image)e.Item.FindControl("Image4");
92 msn.Visible = DB.img("select qq from [guest] where id=" + id + "");
93
94 Image url = (Image)e.Item.FindControl("Image5");
95 url.Visible = DB.img("select url from [guest] where id=" + id + "");
96
97 Image ip = (Image)e.Item.FindControl("Image6");
98 ip.Visible = DB.img("select ip from [guest] where id=" + id + "");
99
100 Label content = (Label)e.Item.FindControl("Label3");
101 Panel dengl = (Panel)e.Item.FindControl("dl");
102
103 //if (DB.img("select pwd from [guest] where id=" + id + ""))
104 //{
105 // //如果有密码,我这里和他做的不一样,前面少了一个 !
106 // //如果要输入密码,则将内容隐藏,出现登录页面
107 // if(Session["fk"]!=null || Session["bk"]!=null)
108 // {
109 // //已经登录情况下,显示内容
110 // //将内容标签赋值。
111 // content.Text = Server.HtmlDecode(DB.scr("select concent from [guest] where id= " + id + ""));
112 // //如果有回复内容,则显示回复信息。
113 // if (Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id + "")) > 0)
114 // {
115 // Panel re = (Panel)e.Item.FindControl("Panel2");
116 // re.Visible = true;
117 // DataList read = (DataList)e.Item.FindControl("DataList2");
118 // read.DataSource = DB.scr("select * from [read] where lid=" + id + "");
119 // read.DataBind();
120
121 // }
122 // }
123 // else
124 // {//需要密码,但是还没登录
125 // dl.Visible=true;
126 // content.Visible=false;
127 // }
128 //}
129 //else
130 //{//无须密码
131
132 // content.Text = Server.HtmlDecode(DB.scr("select concent from [guest] where id= " + id + ""));
133 // //如果有回复内容,则显示回复信息。
134 // if (Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id + "")) > 0)
135 // {
136 // Panel re = (Panel)e.Item.FindControl("Panel2");
137 // re.Visible = true;
138 // DataList read = (DataList)e.Item.FindControl("DataList2");
139 // read.DataSource = DB.scr("select * from [read] where lid=" + id + "");
140 // read.DataBind();
141
142 // }
143
144 //}
145
146 if (!DB.img("select pwd from guest where id=" + id + ""))
147 {//如果没密码的话就直接显示内容以及需要的回复
148 content.Text = Server.HtmlDecode(DB.scr("select concent from guest where id=" + id + "")).ToString();
149 if (Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id + "")) > 0)
150 {//如果有回复的话就把回复panel以及下面的东西都找出来,然后绑上数据,显示出来
151 Panel re = (Panel)e.Item.FindControl("Panel2");
152 re.Visible = true;
153
154 DataList read = (DataList)e.Item.FindControl("DataList2");
155 read.DataSource = DB.ds("select * from [read] where lid=" + id + "");
156 read.DataBind();
157 }
158 }
159 else
160 {//如果有密码就把内容隐去,出现登陆项
161 if (Session["fk"] != null || Session["qx"] != null)
162 {
163 content.Text = Server.HtmlDecode(DB.scr("select concent from guest where id=" + id + "")).ToString();
164 if (Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id + "")) > 0)
165 {//如果有回复的话就把回复panel以及下面的东西都找出来,然后绑上数据,显示出来
166 Panel re = (Panel)e.Item.FindControl("Panel2");
167 re.Visible = true;
168
//下面几行是镶嵌在第一个DataList中的DataList,其实它的绑定真的很简单
169 DataList read = (DataList)e.Item.FindControl("DataList2");
170 read.DataSource = DB.ds("select * from [read] where lid=" + id + "");
171 read.DataBind();
172 }
173 }
174 else
175 {
176 dengl.Visible = true;
177 content.Visible = false;
178 }
179 }
180
181 }
182 }
183 protected void button1_Click(object sender, EventArgs e)
184 {
185 /**/////密码登录查看留言
186 //for (int i = 0; i < DataList1.Items.Count;i++ )
187 //{
188 // TextBox pwd = (TextBox)DataList1.Items[i].FindControl("dlpwd");
189 // Label id = (Label)DataList1.Items[i].FindControl("iidd");
190 // if((DB.scr("select pwd from [guest] where id=" + id.Text)).ToString() == pwd.Text.ToString())
191 // {//登录成功
192 // Session["fk"] = pwd.Text.ToString();
193 // Label content = (Label)DataList1.Items[i].FindControl("Label3");
194 // Panel dl = (Panel)DataList1.Items[i].FindControl("dl");
195
196 // content.Text = Server.HtmlDecode(DB.scr("select concent from [guest] where id=" + id.Text));
197 // content.Visible = true;
198 // dl.Visible = false;
199
200 // //如果有回复内容,则显示回复信息。
201 // if(Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id))>0)
202 // {
203 // Panel re = (Panel)DataList1.Items[i].FindControl("Panel2");
204 // re.Visible = true;
205 // DataList read = (DataList)DataList1.Items[i].FindControl("DataList2");
206 // read.DataSource=DB.scr("select * from [read] where lid=" + id);
207 // read.DataBind();
208 // }
209
210 // }
211 //}
212
213 for (int i = 0; i < DataList1.Items.Count; i++)
214 {
215 TextBox pwd = (TextBox)DataList1.Items[i].FindControl("dlpwd");
216 Label id = (Label)DataList1.Items[i].FindControl("iidd");
217 if (DB.scr("select [pwd] from [guest] where id=" + id.Text + "").ToString() == pwd.Text.ToString())
218 {
219 Session["fk"] = pwd.Text.ToString();
220
221 Label concent = (Label)DataList1.Items[i].FindControl("Label4");
222 Panel dengl = (Panel)DataList1.Items[i].FindControl("dl");
223
224 dengl.Visible = false;
225 concent.Visible = true;
226 if (Convert.ToInt32(DB.scr("select count(*) from [read] where lid=" + id.Text + "")) > 0)
227 {//如果有回复的话就把回复panel以及下面的东西都找出来,然后绑上数据,显示出来
228 Panel re = (Panel)DataList1.Items[i].FindControl("Panel2");
229 re.Visible = true;
230
231 DataList read = (DataList)DataList1.Items[i].FindControl("DataList2");
232 read.DataSource = DB.ds("select * from [read] where lid=" + id.Text + "");
233 read.DataBind();
234 }
235 }
236 }
237 }
238}
239