最近用网奇iwms系统做网站,考虑到浏览者的方便,做了一个“我浏览过的文章”的插件,演示地址:四川艺考网 http://***,具体过程如下:
1.在VS中打开网站,指向路径:D:\wwwroot\scykw.cn,此路径下有iwms的系统。
2.新建项目:IWMS_Plugin,新建类TopicInfo,用于浏览历史的类,编写如下代码:
[Serializable]
public class TopicInfo
{
public TopicInfo()
{
}
private string m_PageURL = "";
private string m_PageTitle = "";
public string PageURL
{
get { return m_PageURL; }
set { m_PageURL = value; }
}
public string PageTitle
{
get { return m_PageTitle; }
set { m_PageTitle = value; }
}
}
3.新建类:TopicInfoList,代码如下:
[Serializable]
public class TopicInfoList : System.Collections.ArrayList, System.ICloneable
{
public TopicInfoList()
{
}
private Hashtable m_IOInfoHash = new Hashtable();
public virtual int Add(TopicInfo info)
{
if (this.m_IOInfoHash.ContainsKey(info.PageURL))
return 0;
this.m_IOInfoHash.Add(info.PageURL, info);
return base.Add(info);
}
public new virtual void Clear()
{
this.m_IOInfoHash.Clear();
base.Clear();
}
}
4.新建 类History,用于历史记录有服务:
public class History
{
public History()
{
}
public static void AddHistory(TopicInfo info)
{
TopicInfoList oldlist = (TopicInfoList)HttpContext.Current.Session["History"];
if (HttpContext.Current.Session["History"] == null)
{
TopicInfoList list = new TopicInfoList();
list.Add(info);
HttpContext.Current.Session["History"] = list;
}
else
{
if (!oldlist.Contains(info))
{
oldlist.Add(info);
}
}
}
public static TopicInfoList GetHistory()
{
TopicInfoList oldlist = (TopicInfoList)HttpContext.Current.Session["History"];
if (oldlist != null)
return oldlist;
return new TopicInfoList();
}
public static int ClearHistory()
{
try
{
TopicInfoList list = History.GetHistory();
list.Clear();
return 1;
}
catch
{
return 0;
}
}
}
5.在网站项目中新建用户自定义控件:History.ascx,代码如下:
<%@ Control Language="C#" Inherits="IWMS_Plugin.HistoryHelp" %>
<script src="inc/jquery.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(function()
{
AddHistory();
getHistory();
});
function ClearHistory()
{
$("#btn_clear").val("正在删除...");
var callback=function(obj)
{
if(obj.value>0)
{
$("#HistoryList").html("");
}else{alert("清除失败");}
$("#btn_clear").val("清空历史记录");
};
IWMS_Plugin.HistoryHelp.ClearHistory(callback);
}
function AddHistory()
{
var callback=function(obj)
{
getHistory();
};
IWMS_Plugin.HistoryHelp.AddHistory(window.location.href,$("title").html(),callback);
}
function getHistory()
{
var callback=function(obj)
{
if(obj.value!=null)
{
$("#HistoryList").html(obj.value);
}else{alert("读取失败");}
};
IWMS_Plugin.HistoryHelp.getHistory(callback);
}
</script>
<style type="text/css">
#HistoryList,#HistoryList li{margin:0px;padding:0px;list-style:none;}
#HistoryList li a{display:block;padding:2px;border:1px solid #fff;margin:2px;text-decoration:none;}
#HistoryList li a:hover{background:#eeeeee;border:1px solid #ccc;}
</style>
<input type="button" value="清空历史记录" οnclick="ClearHistory()" id="btn_clear"/>
<ul id="HistoryList">
</ul>
5. IWMS_Plugin.HistoryHelp类代码如下:
public class HistoryHelp : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(HistoryHelp), this.Page);
}
[AjaxPro.AjaxMethod]
public static string getHistory()
{
StringBuilder sb = new StringBuilder();
TopicInfoList list = History.GetHistory();
foreach (TopicInfo info in list)
{
sb.Append(String.Format("<li><a href=\"{0}\" title=\"{1}\">{1}</a></li>",
info.PageURL,
info.PageTitle
));
}
return sb.ToString();
}
[AjaxPro.AjaxMethod]
public void AddHistory(string url,string title)
{
TopicInfo info = new TopicInfo();
info.PageURL = url;
info.PageTitle = title;
History.AddHistory(info);
}
[AjaxPro.AjaxMethod]
public int ClearHistory()
{
return History.ClearHistory();
}
}
大功告成!!系统演示请见:***