在用到.net自带的gridview实现分页时,很难把握住帖子样子的实现。尝试了用手工书写的形式实现。
部分代码如下(前台)
1. <!--帖子的有关内容-->
2. <div style="width: 970px; height: auto; text-align: left;">
3. <asp:Table ID="tietable" runat="server" Width="100%" BorderWidth="1px" BorderColor="Gray" GridLines="Horizontal">
4. <asp:TableRow>
5. <asp:TableCell Width="55%" Text="标题"></asp:TableCell>
6. <asp:TableCell Width="15%" Text="发帖人/发帖时间"></asp:TableCell>
7. <asp:TableCell Width="15%" Text="回复量/浏览量"></asp:TableCell>
8. <asp:TableCell Text="最后一次访问时间"></asp:TableCell>
9. </asp:TableRow>
10. </asp:Table>
11.</div>
12.<!--发帖按钮-->
13.<div style="width: 970px; height: 30px; text-align: left; vertical-align: middle; line-height: 30px;">
14. <div style="width: 120px; float: left">
15. <asp:ImageButton ID="fatie" runat="server" ImageUrl="image/zhuye_button.gif" OnClick="imagebutton1_click" /></div>
16. <!--这里可以添加页数指引-->
17. <table cellpadding="0" cellspacing="0" style="float: right;">
18. <tr>
19. <td style="width: 120px;">
20. <asp:Label ID="label5" runat="server" Font-Size="10pt" Text="当前页为【"></asp:Label>
21. <asp:Label ID="label7" runat="server" Font-Size="10pt" Text="1"></asp:Label>
22. <asp:Label ID="label3" runat="server" Font-Size="10pt" Text="】"></asp:Label>
23. </td>
24. <td style="width: 40px;">
25. <asp:LinkButton ID="linkbutton2" runat="server" Font-Size="10pt" ForeColor="red" OnClick="linkbutton2_click">首页</asp:LinkButton>
26. </td>
27. <td style="width: 30px;">
28. <asp:LinkButton ID="linkbutton3" Width="42px" runat="server" Font-Size="10pt" ForeColor="red" OnClick="linkbutton3_click">上一页</asp:LinkButton>
29. </td>
30. <td style="width: 48px;">
31. <asp:LinkButton ID="linkbutton4" runat="server" Font-Size="10pt" ForeColor="red" Width="48px" OnClick="linkbutton4_click">下一页</asp:LinkButton>
32. </td>
33. <td style="width: 49px;">
34. <asp:LinkButton ID="linkbutton5" runat="server" Font-Size="10pt" ForeColor="red" Width="29px" OnClick="linkbutton5_click">尾页</asp:LinkButton>
35. </td>
36. <td style="width: 120px;" align="center">
37.<asp:Label ID="label1" runat="server" Text="总页数【"Font-Size="10pt"Width="52px"></asp:Label>
38. <asp:Label ID="label2" runat="server" Font-Size="10pt"></asp:Label>
39. <asp:Label ID="label4" runat="server" Font-Size="10pt" Text="】"></asp:Label>
40. </td>
41. </tr>
42. </table>
43.</div>
44.<!--隐藏的发帖table-->
45.<asp:Table ID="fatiebgd" runat="server" Width="961px" Height="200px" GridLines="Both" HorizontalAlign="center" BackColor="green" Visible="false" Style="margin: 0px">
46. <asp:TableRow Width="1000px">
47. <asp:TableCell>
48. <asp:Table ID="fatietable" runat="server" Width="961px" BorderWidth="1px" Height="200px" GridLines="Both" BorderColor="LightYellow" BorderStyle="Solid" BackColor="white">
49. <asp:TableRow>
50. <asp:TableCell ID="TableCell1" runat="server" Width="15%" Height="20px" Text="标题:"></asp:TableCell>
51. <asp:TableCell ID="TableCell2" runat="server" HorizontalAlign="Left">
52. <asp:TextBox runat="server" ID="headline" Width="85%"></asp:TextBox></asp:TableCell>
53. </asp:TableRow>
54. <asp:TableRow>
55. <asp:TableCell ID="TableCell3" runat="server" Width="15%" Height="160px" Text="内容:" VerticalAlign="Middle"></asp:TableCell>
56. <asp:TableCell ID="TableCell4" runat="server">
57. <asp:TextBox Width="99%" runat="server" ID="postscontents" TextMode="MultiLine" Rows="9"></asp:TextBox></asp:TableCell>
58. </asp:TableRow>
59. <asp:TableRow>
60. <asp:TableCell ColumnSpan="2" HorizontalAlign="center">
61. <asp:Button ID="OK" Text="提交" Width="80px" runat="server" OnClick="ok_click" />
62. <asp:Button ID="kong" runat="server" Width="80px" Text="重置" OnClick="kong_click" />
63. </asp:TableCell>
64. </asp:TableRow>
65. </asp:Table>
66. </asp:TableCell></asp:TableRow>
67.</asp:Table>
(后台代码)
1. protected void Page_Load(object sender, EventArgs e)
2. {
3. if (!IsPostBack)
4. {
5. dbBind();
6. }
7. }
8.
9. protected void dbBind()
10. {
11. SqlConnection conn = new SqlConnection(CommonMethods.connstring);
12. SqlCommand cmd = new SqlCommand("select 发帖时间 from title", conn);
13. SqlDataReader rd = null;
14. conn.Open();
15. rd = cmd.ExecuteReader();
16. int rowdata = 0;
17. while (rd.Read())
18. {
19. rowdata++;
20. }
21. conn.Close();
22. int allpage = 0;
23. if (rowdata / 3 == 0)
24. {
25. allpage = rowdata / 3;
26. }
27. else
28. {
29. allpage = rowdata / 3 + 1;
30. }
31. this.label2.Text = allpage.ToString();
32. int curpage = Convert.ToInt32(this.label7.Text);
33. this.linkbutton2.Enabled = true;
34. this.linkbutton3.Enabled = true;
35. this.linkbutton4.Enabled = true;
36. this.linkbutton5.Enabled = true;
37. if (curpage == 1)//如果等于1则上一页和首页禁用
38. {
39. this.linkbutton2.Enabled = false;
40. this.linkbutton3.Enabled = false;
41. }
42. if (curpage == Convert.ToInt32(this.label2.Text))//如果等于尾页则下一页和尾页链接禁用
43. {
44. this.linkbutton4.Enabled = false;
45. this.linkbutton5.Enabled = false;
46. }
47. string sql = "select top 3 标题,用户名,发帖时间,回复量,浏览量,最后一次访问时间 from title where 最后一次访问时间 NOT IN (select top " + 3 * (curpage - 1) + " 最后一次访问时间 from title order by 最后一次访问时间 desc) order by 最后一次访问时间 desc";
48. SqlCommand command = new SqlCommand(sql, conn);
49. conn.Open();
50. rd = command.ExecuteReader();
51. for (int i = 1; i < tietable.Rows.Count; i++)
52. {
53. tietable.Rows[i].Cells.Clear();
54. }
55. while (rd.Read())
56. {
57. TableRow row = new TableRow();
58. TableCell c1 = new TableCell();
59. TableCell c2 = new TableCell();
60. TableCell c3 = new TableCell();
61. TableCell c4 = new TableCell();
62. c1.Text = rd["标题"].ToString();
63. c2.Text = rd["用户名"].ToString() + "<br/>" + rd["发帖时间"].ToString();
64. c3.Text = rd["回复量"].ToString() + "/" + rd["浏览量"].ToString();
65. c4.Text = rd["最后一次访问时间"].ToString();
66. row.Cells.Add(c1);
67. row.Cells.Add(c2);
68. row.Cells.Add(c3);
69. row.Cells.Add(c4);
70. tietable.Rows.Add(row);
71. }
72. }
73.
74. protected void linkbutton2_click(object sender, EventArgs e)
75. {
76. this.label7.Text = "1";
77. this.dbBind();
78. }
79.
80. protected void linkbutton3_click(object sender, EventArgs e)
81. {
82. this.label7.Text = Convert.ToString(Convert.ToUInt32(this.label7.Text) - 1);
83. this.dbBind();
84. }
85.
86. protected void linkbutton4_click(object sender, EventArgs e)
87. {
88. this.label7.Text = Convert.ToString(Convert.ToInt32(this.label7.Text) + 1);
89. this.dbBind();
90. }
91.
92. protected void linkbutton5_click(object sender, EventArgs e)
93. {
94. this.label7.Text = this.label2.Text;
95. this.dbBind();
96. }
97. protected void imagebutton1_click(object sender, EventArgs e)
98. {
99. fatiebgd.Visible = true;//让发帖的表格显示在网页中
100. }
101. protected void ok_click(object sender, EventArgs e)
102. {
103. if (headline.Text==null||headline.Text==""||postscontents.Text==null||postscontents.Text=="")
104. {
105. Response.Write("<script>alert('标题和内容任一项都不能为空!');</script>");
106. }
107. }
108. protected void kong_click(object sender, EventArgs e)
109. {
110. headline.Text = "";
111. postscontents.Text = "";
112. }