使用JQuery开发显示RSS Feed的WebPart

来源:互联网 发布:谷歌断点调试js 编辑:程序博客网 时间:2024/04/29 11:45

由于公司里面正在使用WSS来创建内部网,其中有些讯息比如公司新闻、人事通知、招聘启事等,都会由相关的部门在相应的List里面输入讯息,然后在指定的地方显示出来。由于同一个List有可能在多个地方显示,虽然WSS的List默认都有相应的显示和编辑页面,但是显示的格式和方式都是默认的,不太容易改变和定制。因此我考虑自己开发一个WebPart,在需要显示的页面里面添加该WebPart,然后指定该WebPart的RSS的地址就可以了。这个方法也是我们自己摸出来的,不知各位童鞋有没有更好的方式可以交流一下?下面具体列出各个步骤,以备今后参考:

 

 

1、在VS2008里面创建WebPart的工程项目; 

 

 创建WebPart工程

 

2、把VS2008默认创建的WebPart1删除掉;

 创建WebPart工程2

 

3、添加“新建项”,准备创建新的WebPart类;

 

 添加新项,准备创建新的WebPart类

 

4、选择“Web部件”;

 选择WebPart部件的模板

 

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框架的功能了。

 

 添加jQuery框架和基于jQuery的jfeed的Plugin

 

7、

 

 设置js文件的属性

 

8、

设置js文件的属性为“嵌入的资源”

 

9、

 打开AssemlyInfo文件

 

 

10、

 

 声明嵌入的资源文件

 

11、

 

 部署WebPart工程

 

12、

 

添加WebPart到页面上

 

13、

设置RSS Feed的URL

 

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;
        }
    }
}

 

 

 

 

原创粉丝点击