通用新闻文章系统标题列表用户控件,大大减少重复劳动

公司网站改版,使用了.net2.0,其中有大量的新闻文章系统的标题列表方式显示,算是最简单重复的劳动了,功能莫过于显示标题,截断标题,分类id,是否显示时间和点击次数等等,于是就构思着做了这个用户控件,使用很方便,只需要设置几个属性就可以工作了,不知道对你是否有价值呢?

ArticlesTitleList.ascx源代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ArticlesTitleList.ascx.cs" Inherits="UserControls_ArticlesTitleList" EnableViewState="false"%>

<asp:Repeater ID="Repeater1" runat="server" EnableViewState="False">
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
<li><%# DataBinder.Eval(Container.DataItem, "TheTitle")%><%# DataBinder.Eval(Container.DataItem, "EndInfo")%></li>

</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
<asp:Literal ID="ltlMore" runat="server"></asp:Literal>

ArticlesTitleList.ascx.cs源代码:
(其中用到了一个自己编写的数据库操作类DataOperater ,可以替换为你自己的)


using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using cnfdc;

public partial class UserControls_ArticlesTitleList : System.Web.UI.UserControl
{
private string classId = "0";//要显示的文章分类ID
private bool imgOnly = false;//是否是显示含有图片的文章
private string itemsCount = "10";//要显示的文章标题的数量
private string titleLength="0";//控制标题长度的数值,如设置为显示类别,则包括类别名称长度在内,0为全部显示
protected string detailsPagePrefix = string.Empty;//要链接到的文章详细页的页面地址,绝对根路径表示
private bool displayClassName = false;//是否在标题前显示类别文章入口,链接到该类别文章列表,string型,如果设为true,则需要同时指定分类文章列表的URL,绝对根路径表示
protected string articleListPageUrl = string.Empty;//分类文章列表页的URL,绝对根路径表示,配合DisplayClassName实现
private string orderType = "latest";//排序类型,分为最新latest、点击hits
private bool displayMore = false;//是否显示更多...链接
private string excludeID = "0";//要排除在外的id,以英文逗号分割
private bool displayTime = false;//是否在标题后显示时间
private string timeFormatString = "&nbsp;&nbsp;[{y}/{m}/{d}]";//时间格式化字符串
private bool displayHits = false;//是否显示点击次数
private string hitsFormatString = "&nbsp;({hits}次)";//点击次数格式化字符串

#region 属性设置
public string ClassId
{
set
{
classId = value;
}
}

public bool ImgOnly
{
set
{
imgOnly = value;
}
}

public string ItemsCount
{
set
{
itemsCount = value;
}
}

public string TitleLength
{
set
{
titleLength = value;
}
}

public string DetailsPagePrefix
{
set
{
detailsPagePrefix = value;
}
}

public bool DisplayClassName
{
set
{
displayClassName = value;
}
}

public string ArticleListPageUrl
{
set
{
articleListPageUrl = value;
}
}

public string OrderType
{
set
{
orderType = value;
}
}

public bool DisplayMore
{
set
{
displayMore = value;
}
}

public string ExcludeID
{
set
{
excludeID = value;
}
}

public bool DisplayTime
{
set
{
displayTime = value;
}
}
public string TimeFormatString
{
set
{
timeFormatString = value;
}
}
public bool DisplayHits
{
set
{
displayHits = value;
}
}
public string HitsFormatString
{
set
{
hitsFormatString = value;
}
}
#endregion

protected void Page_Load(object sender, EventArgs e)
{
DataOperater mydo = new DataOperater();
string sql = string.Empty;
if (displayClassName)
{
sql = "SELECT TOP " + itemsCount + " Articles_Table.id,Articles_Title,Click_Count,Articles_Type,Sort_First,Add_Time,typeName FROM Articles_Table,Articles_Type WHERE id NOT IN (" + excludeID + ") AND Verify_Status=1 AND Articles_Type.id=Articles_Table.Articles_Type AND Articles_Type=" + classId;
}
else
{
sql = "SELECT TOP " + itemsCount + " id,Articles_Title,Click_Count,Articles_Type,Sort_First,Add_Time FROM Articles_Table WHERE id NOT IN (" + excludeID + ") AND Verify_Status=1 AND Articles_Type=" + classId;
}

if (imgOnly)
{
sql = sql + "AND Has_Img = 1";
}

if (orderType == "latest")
{
sql = sql + "ORDER BY Add_Time DESC,Sort_First DESC";
}
else
{
sql = sql + "ORDER BY Click_Count DESC";
}

DataSet ds = mydo.DataSet(sql,"ArticlesTitleList");

//处理标题截断并加入时间显示和点击次数显示
DataColumn dc0 = new DataColumn("CuttedTitle");
dc0.DataType = System.Type.GetType("System.String");
dc0.DefaultValue = "";
ds.Tables["ArticlesTitleList"].Columns.Add(dc0);
DataColumn dc3 = new DataColumn("EndInfo");
dc3.DataType = System.Type.GetType("System.String");
dc3.DefaultValue = "";
ds.Tables["ArticlesTitleList"].Columns.Add(dc3);

for (int j = 0; j < ds.Tables["ArticlesTitleList"].Rows.Count; j++)
{
DataRow dr = ds.Tables["ArticlesTitleList"].Rows[j];
dr.BeginEdit();

if (titleLength == "0")//不截断
{
dr["CuttedTitle"] = dr["Articles_Title"].ToString();
}
else
{
int classNameLength = 0;
if (displayClassName)
{
classNameLength = LeftTrue.GetLength(dr["typeName"].ToString());
}

dr["CuttedTitle"] = LeftTrue.CutString(dr["Articles_Title"].ToString(), int.Parse(titleLength) - classNameLength);
}

//时间显示
if (displayTime)
{
string timeString = timeFormatString;
timeString = timeString.Replace("{y}", Convert.ToDateTime(dr["Add_Time"]).Year.ToString());
timeString = timeString.Replace("{m}", Convert.ToDateTime(dr["Add_Time"]).Month.ToString());
timeString = timeString.Replace("{d}", Convert.ToDateTime(dr["Add_Time"]).Day.ToString());
dr["EndInfo"] =dr["EndInfo"].ToString()+ timeString;
}

//点击次数显示
if (displayHits)
{
string hitString = hitsFormatString;
hitString = hitString.Replace("{hits}", dr["Click_Count"].ToString());
dr["EndInfo"] = dr["EndInfo"].ToString() + hitString;
}
dr.EndEdit();
}

//按照是否显示分类名和链接自定义标题列
DataColumn dc1 = new DataColumn("TheTitle");
dc1.DataType = System.Type.GetType("System.String");
dc1.DefaultValue = "";
ds.Tables["ArticlesTitleList"].Columns.Add(dc1);

//给每一行赋值
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
DataRow dr = ds.Tables[0].Rows[j];
dr.BeginEdit();

if (!displayClassName)
{
dr["TheTitle"] = "<a title=\""+dr["Articles_Title"].ToString()+"\" href=\""
+ detailsPagePrefix + "_"
+ dr["id"].ToString() + ".html\" target=\"_blank\">" + dr["CuttedTitle"].ToString() + "</a>";
}
else
{
dr["TheTitle"] = "<a title=\"点击阅读更多该类资讯\" href=\"" + articleListPageUrl + "?ClassId="
+ dr["Articles_Type"].ToString() + "\" target=\"_blank\" >[" + dr["typeName"].ToString() + "]</a><a title=\""
+ dr["Articles_Title"].ToString() + "\" href=\"" + detailsPagePrefix + "?ArticelId="
+ dr["id"].ToString() + "\" target=\"_blank\">" + dr["CuttedTitle"].ToString() + "</a>";
}
dr.EndEdit();
}

Repeater1.DataSource = ds;
Repeater1.DataBind();
ds.Dispose();
mydo.CloseConnection();

if (displayMore)
{
ltlMore.Text = "<div align=\"right\"><a href=\"" + articleListPageUrl + "?ClassId=" + classId + "\" target=\"_blank\">+更多</a></div>";
}
}
}

你可能感兴趣的:(sql,UI,Web,SQL Server,asp)