显示上一条新闻 下一条新闻

显示上一条新闻 下一条新闻 这个经常在新闻的内容的下面会显示出来,方便用户浏览,这个很好实现,看下面的sql语句就知道了

假设当前NewsID=2 select NewsTitle from NewsTable where NewsID= (select top 1 NewsID from NewsTable where NewsID>2 order by NewsID asc) or NewsID=(select top 1 NewsID from NewsTable where NewsID<2 order by NewsID desc)

这个时候我们就会很明白

上一条新闻就是

select NewsTitle from NewsTable where NewsID=(select top 1 NewsID from NewsTable where NewsID<2 order by NewsID desc)

下一条新闻就是

select NewsTitle from NewsTable where NewsID=(select top 1 NewsID from NewsTable where NewsID>2 order by NewsID asc)

到时候写个方法,将当前的id作为参数就可以很好的是实现了!

 

下面是自己总结的代码,到时候直接套用:

数据层:

View Code
    /// <summary>
        
/// 上一篇  下一篇
        
/// </summary>
        
/// <param name="id"></param>
        
/// <param name="strOrder"></param>
        
/// <param name="FH"></param>
        
/// <returns></returns>
        public Jiu.Model.Lt_Article GetTitle(int id,string btype,string strOrder, string FH)
        {
            StringBuilder strSql 
= new StringBuilder();
            strSql.Append(
"select top 1 id ,title from Lt_Article where");
            strSql.Append(
" id" + FH + id + " and B_Sort="+btype+"  order by " + strOrder);
            Jiu.Model.Lt_Article model 
= new Jiu.Model.Lt_Article();
            DataSet ds 
= DbHelperOleDb.Query(strSql.ToString());
            
if (ds.Tables[0].Rows.Count > 0)
            {
                
if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id 
= int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.Title 
= ds.Tables[0].Rows[0]["title"].ToString();
                
return model;
            }
            
else
            {
                
return null;
            }

        }

表示层:

页面:

 

View Code
 <div class="productd2">
                            
<div style="width:100%; text-align:center; margin-top:80px; margin-bottom:10px"><span runat="server" id="Up"><span style="font-size:14px; color:Green">前一篇:</span><asp:Label ID="lblUp" runat="server" Text="Label"></asp:Label></span> <span style="margin-left:40px" runat="server" id="Down"><span style="font-size:14px; color:Green">后一篇:</span><asp:Label ID="lblDown" runat="server" Text="Label"></asp:Label></span></div>
                    
</div>

cs代码

 

View Code
 //上一篇 后一篇
    public void UpAndDownNews(int bid, int id)
    {
        Jiu.Model.Lt_Article NewsInfoDown 
= NewsManager.GetTitle(id,bid.ToString(), "id desc""<");
        Jiu.Model.Lt_Article NewsInfoUp 
= NewsManager.GetTitle(id,bid.ToString(), "id asc"">");
        
if (NewsInfoUp != null)
        {
            
this.lblUp.Text = "<a href='NewInfo.aspx?bid="+bid+"&id=" + NewsInfoUp.Id + "' title='" + NewsInfoUp.Title + "'>" + getTitle(NewsInfoUp.Title) + "</a>";
        }
        
else
        {
            Up.Visible 
= false;
        }
        
if (NewsInfoDown != null)
        {
            
this.lblDown.Text = "<a href='NewInfo.aspx?bid=" + bid + "&id=" + NewsInfoDown.Id + "' title='" + NewsInfoDown.Title + "'>" + getTitle(NewsInfoDown.Title) + "</a>";
        }
        
else
        {
            Down.Visible 
= false;
        }

    }

 

posted @ 2011-03-04 14:36  双魂人生  阅读(1706)  评论(0编辑  收藏  举报