由于公司裡面正在使用WSS來建立内部網,其中有些訊息比如公司新聞、人事通知、招聘啟事等,都會由相關的部門在相應的List裡面輸入訊息,然後在指定的地方顯示出來。由于同一個List有可能在多個地方顯示,雖然WSS的List預設都有相應的顯示和編輯頁面,但是顯示的格式和方式都是預設的,不太容易改變和定制。是以我考慮自己開發一個WebPart,在需要顯示的頁面裡面添加該WebPart,然後指定該WebPart的RSS的位址就可以了。這個方法也是我們自己摸出來的,不知各位童鞋有沒有更好的方式可以交流一下?下面具體列出各個步驟,以備今後參考:
1、在VS2008裡面建立WebPart的工程項目;

2、把VS2008預設建立的WebPart1删除掉;
3、添加“建立項”,準備建立新的WebPart類;
4、選擇“Web部件”;
5、VS2008自動建立了WebPart的相關檔案,此時的WebPart已經可以運作了,隻不過輸出的是最基本的内容,并沒有任何的功能。
6、
要在WebPart裡面使用jQuery架構的話,必須在WebPart運作的Page裡面加上jQuery檔案的引用;有兩種方式可以添加jQuery架構的引用,一種是利用MasterPage的特性,在default.master檔案的Head裡面用<script 标簽聲明jQuery的引用;另外一種方式是把jQuery的js檔案當成WebPart的嵌入入的資源,這樣編譯器會把js檔案一起編譯到WebPart的dll裡面,這樣做的好處是釋出WebPart的時候簡單一些,但是如果在同一個頁面上面有多個WebPart的執行個體,那麼這些js檔案會被重複的引用多次,這樣會造成頁面加載的速度變慢。是以我在這裡選擇把經常會使用到的jquery-1.3.2.js放在WSS伺服器的_layouts/javascript目錄裡面,然後再default.master檔案裡面的Head部分添加以下語句<script type='"text/javascript" src="/_layouts/javascript/jquery-1.3.2.js></script>"來引用該檔案,這樣每個基于default.master的頁面都可以使用得到jQuery架構的功能了。
7、
8、
9、
10、
11、
12、
13、
14、
jQueryManager.cs檔案:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
using System.Collections.ObjectModel;
namespace jQuery.ScriptManager
{
[ParseChildren(true)]
public class jQueryManager : WebControl
{
private ITemplate scriptTemplate;
private LiteralControl scriptLiteral = new LiteralControl();
private List<StartFunction> _readyFunctions = new List<StartFunction>();
[Browsable(true), DefaultValue(null), PersistenceMode(PersistenceMode.InnerProperty)]
public List<StartFunction> ReadyFunctions
{
get
{
return _readyFunctions;
}
set
{
_readyFunctions = value;
}
}
private List<ScriptBlock> _otherFunctions;
[Browsable(true), DefaultValue(null), PersistenceMode(PersistenceMode.InnerProperty)]
public List<ScriptBlock> Scripts
{
get
{
return _otherFunctions;
}
set
{
_otherFunctions = value;
}
}
//http://blog.iridescence.no/Posts/EnsuringaSingleInstanceofaControlonaPage.aspx
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page page = this.Page;
if (page != null)
{
// check if an instance has already been added to the page
if (page.Items.Contains(typeof(jQueryManager)))
{
throw new InvalidOperationException("Only one instance of the jQueryManager control can be placed on a Page");
}
// add a reference to the Items collection for easy reference to the control; GetCurrent() will use this
page.Items[typeof(jQueryManager)] = this;
}
if (scriptTemplate != null)
{
scriptTemplate.InstantiateIn(this);
}
Page.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
//Page.ClientScript.RegisterClientScriptInclude("jqueryScript", Page.ClientScript.GetWebResourceUrl(typeof(jQueryManager), "EsquelRssReader.js.jquery-1.3.2.js"));
}
protected override void AddParsedSubObject(object obj)
{
if (obj is LiteralControl)
{
RegisterStartFunction(((LiteralControl)obj).Text);
}
else
{
base.AddParsedSubObject(obj);
}
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
StringBuilder Start = new StringBuilder();
if (Scripts != null)
{
foreach (ScriptBlock r in Scripts)
Start.Append(r.JSScriptBlock + Environment.NewLine);
}
if (ReadyFunctions != null)
{
Start.Append("$(document).ready(function(){");
foreach (StartFunction r in ReadyFunctions)
Start.Append(r.FunctionName + Environment.NewLine);
Start.Append("});/n/n");
}
Page.ClientScript.RegisterClientScriptBlock(typeof(jQueryManager), "JQuery", Start.ToString(), true);
}
protected override void OnPreRender(EventArgs e)
{
base.OnLoad(e);
}
/// <summary>
/// Gets the instance of the jQueryManager on the page
/// </summary>
/// <param name="page">Current Page</param>
/// <returns>Instance of the jQueryManager</returns>
public static jQueryManager GetCurrent(Page page)
{
if (page == null)
{
throw new ArgumentNullException("page");
}
jQueryManager manager = page.Items[typeof(jQueryManager)] as jQueryManager;
return manager;
}
/// <summary>
/// Registers a script
/// </summary>
/// <param name="Script">The full contents of the script (function name(){ ... })</param>
public void RegisterScript(string Script)
{
if (_otherFunctions == null)
_otherFunctions = new List<ScriptBlock>();
_otherFunctions.Add(new ScriptBlock(Script));
}
/// <summary>
/// Registers the name of a function to call on $(document).ready
/// </summary>
/// <param name="FunctionName">The name of the function</param>
public void RegisterStartFunction(string FunctionName)
{
if (_readyFunctions == null)
_readyFunctions = new List<StartFunction>();
Boolean pass = true;
foreach (StartFunction reg in _readyFunctions)
{
if (reg.FunctionName == FunctionName)
{
pass = false;
}
}
if (pass)
{
_readyFunctions.Add(new StartFunction(FunctionName));
}
}
[PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(TemplateControl))]
public ITemplate ScriptTemplate
{
get { return scriptTemplate; }
set { scriptTemplate = value; }
}
}
}
ScriptBlock.cs檔案:
using System;
using System.Collections.Generic;
using System.Text;
namespace jQuery.ScriptManager
{
public class ScriptBlock
{
private string _scriptBlock;
public string JSScriptBlock
{
get
{
return _scriptBlock;
}
set
{
_scriptBlock = value;
}
}
public ScriptBlock()
{
}
public ScriptBlock(string Script)
{
_scriptBlock = Script;
}
}
}
StartFunction.cs檔案:
using System;
using System.Collections.Generic;
using System.Text;
namespace jQuery.ScriptManager
{
public class StartFunction
{
private string _functionName = String.Empty;
public string FunctionName
{
get
{
return _functionName;
}
set
{
_functionName = value;
}
}
public StartFunction()
{
}
public StartFunction(string Start)
{
_functionName = Start;
}
}
}