文章的上一篇和下一篇导航
当浏览文章时,希望在文章下面有一个导航,就是上一篇和下一篇的功能。可从下三张截图看到效果:
要SQL查询语句前,就须把上一篇和下一篇的文章ID找出来,下面的SQL语句有些字段已经省略。
View Code
SELECT [ArticleId],[Subject],
(SELECT MAX([ArticleId]) FROM [dbo].[Article] AS a1 WHERE a1.[ArticleId] < a.[ArticleId]) AS [PrevArticleId],
(SELECT MIN([ArticleId]) FROM [dbo].[Article] AS a1 WHERE a1.[ArticleId] > a.[ArticleId]) AS [NextArticleId]
FROM [dbo].[Article] AS a
(SELECT MAX([ArticleId]) FROM [dbo].[Article] AS a1 WHERE a1.[ArticleId] < a.[ArticleId]) AS [PrevArticleId],
(SELECT MIN([ArticleId]) FROM [dbo].[Article] AS a1 WHERE a1.[ArticleId] > a.[ArticleId]) AS [NextArticleId]
FROM [dbo].[Article] AS a
执行结果:
数据库方面完成了,接下来在asp.net实现,可以首先创建一个用户控件:
ArticleNavigation.ascx:
View Code
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ArticleNavigation.ascx.vb" Inherits="SiteControls_ArticleNavigation" %>
上一篇:<asp:HyperLink ID="HyperLinkPrev" runat="server" Target="_blank"></asp:HyperLink> <br />
下一篇:<asp:HyperLink ID="HyperLinkNext" runat="server" Target="_blank"></asp:HyperLink>
上一篇:<asp:HyperLink ID="HyperLinkPrev" runat="server" Target="_blank"></asp:HyperLink> <br />
下一篇:<asp:HyperLink ID="HyperLinkNext" runat="server" Target="_blank"></asp:HyperLink>
ArticleNavigation.ascx.vb:
View Code
Imports System.Data
Imports Insus.NET
Partial Class SiteControls_ArticleNavigation
Inherits System.Web.UI.UserControl
Dim objArticle As New Article()
Private _PrevArticleId As String
Private _NextArticleId As String
Public WriteOnly Property PrevArticleId As String
Set(value As String)
_PrevArticleId = value
End Set
End Property
Public WriteOnly Property NextArticleId As String
Set(value As String)
_NextArticleId = value
End Set
End Property
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Data_Binding()
End Sub
Private Sub Data_Binding()
If String.IsNullOrEmpty(_PrevArticleId) Then
Me.HyperLinkPrev.Text = "无"
Else
Dim objPrevDataRow As DataRow = objArticle.GetArticleByPrimaryKey(_PrevArticleId).Rows(0)
Me.HyperLinkPrev.Text = objPrevDataRow("Subject")
Me.HyperLinkPrev.NavigateUrl = "~/ArticleView.aspx?ID=" & _PrevArticleId
End If
If String.IsNullOrEmpty(_NextArticleId) Then
Me.HyperLinkNext.Text = "无"
Else
Dim objNextDataRow As DataRow = objArticle.GetArticleByPrimaryKey(_NextArticleId).Rows(0)
Me.HyperLinkNext.Text = objNextDataRow("Subject")
Me.HyperLinkNext.NavigateUrl = "~/ArticleView.aspx?ID=" & _NextArticleId
End If
End Sub
End Class
Imports Insus.NET
Partial Class SiteControls_ArticleNavigation
Inherits System.Web.UI.UserControl
Dim objArticle As New Article()
Private _PrevArticleId As String
Private _NextArticleId As String
Public WriteOnly Property PrevArticleId As String
Set(value As String)
_PrevArticleId = value
End Set
End Property
Public WriteOnly Property NextArticleId As String
Set(value As String)
_NextArticleId = value
End Set
End Property
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Data_Binding()
End Sub
Private Sub Data_Binding()
If String.IsNullOrEmpty(_PrevArticleId) Then
Me.HyperLinkPrev.Text = "无"
Else
Dim objPrevDataRow As DataRow = objArticle.GetArticleByPrimaryKey(_PrevArticleId).Rows(0)
Me.HyperLinkPrev.Text = objPrevDataRow("Subject")
Me.HyperLinkPrev.NavigateUrl = "~/ArticleView.aspx?ID=" & _PrevArticleId
End If
If String.IsNullOrEmpty(_NextArticleId) Then
Me.HyperLinkNext.Text = "无"
Else
Dim objNextDataRow As DataRow = objArticle.GetArticleByPrimaryKey(_NextArticleId).Rows(0)
Me.HyperLinkNext.Text = objNextDataRow("Subject")
Me.HyperLinkNext.NavigateUrl = "~/ArticleView.aspx?ID=" & _NextArticleId
End If
End Sub
End Class
在文章浏览的aspx中,用户控控件需要传入前一篇的文章ID和下一篇的文章ID,下面的PrevArticleId和NextArticleId两个都是用户控件的public的属性。
View Code
<uc1:ArticleNavigation ID="ArticleNavigation1" runat="server" PrevArticleId='<%# Eval("PrevArticleId")%>'
NextArticleId='<%# Eval("NextArticleId")%>' />
NextArticleId='<%# Eval("NextArticleId")%>' />