Asp.net2.0 - WebParts学习笔记(2):WebPart 之间进行静态通讯的完整示例(附源码)

上一篇介绍了Web Parts基础,如果两个Web Part之间不能通讯,那就相当郁闷的,所以Asp.Net提供了两种方式来使得Web Part之间可以互相通讯,一种是静态通讯方式,另一种时使用ConnectionZone进行动态通讯。(这里所谓的“Web Part通讯”是指多个WebPart中用户控件之间的通讯)。下面是Web Parts直接进行通讯的模型:


    我在学习过程中,通过实例--根据我的Blog的RSS来搜索文章--来演示Web Part之间的静态通讯。运行结果如下所示(没有进行界面美化^_^):
起始页面:


    以"struct"作为关键字搜索的结果页面:


    页面中放了两个Web Part,第一个WebPart中包含一个由文本框和按钮组成的用户控件Search(这里用的是用户控件,而不是直接在WebPart放置文本框和按钮,如果直接放的话每个控件都有个标题条TitleBar,感觉太别扭);第二个WebPart中负责显示搜索结果,也是包含一个用户控件Content,Content控件包含0个或多个动态添加的Label。下面是设计Web Parts间进行通讯的步骤:

1. 创建消息接口:定义一个IMessage接口

1 None.gif public   interface  IKeyWord
2 ExpandedBlockStart.gif {
3ExpandedSubBlockStart.gif string Messageget;}
4ExpandedBlockEnd.gif}

 

2. 通讯提供者(Provider):
     这个示例中,第一个WebPart中的用户控件Search作为通讯提供者,因此我们定义Search控件的代码如下:

 1 None.gif public  partial  class  Search : System.Web.UI.UserControl,IKeyWord // 继承通讯接口IKeyWord
 2 ExpandedBlockStart.gif {
 3InBlock.gif    protected void Page_Load(object sender, EventArgs e)
 4ExpandedSubBlockStart.gif    {
 5ExpandedSubBlockEnd.gif    }
 6InBlock.gif
 7ExpandedSubBlockStart.gif    /*通讯提供者,实现方法返回消息接口,并且方法上要运用特性[ConnectionProvider],第二个参数用作在WebPartManager中注册*/
 8InBlock.gif    [ConnectionProvider("KeyWord""KeyWordProvider")]
 9InBlock.gif    public IKeyWord KeyWordProvide()
10ExpandedSubBlockStart.gif    {
11InBlock.gif        return this;
12ExpandedSubBlockEnd.gif    }
13InBlock.gif
14ContractedSubBlock.gif    IKeyWord 成员
20ExpandedBlockEnd.gif}

21 None.gif
22 None.gif




3. 通讯订阅者(Consumer):
     这个示例中,第而个WebPart中的用户控件Content作为消息订阅者,因此我们定义该控件的代码如下:

 1 None.gif public  partial  class  Content : System.Web.UI.UserControl
 2 ExpandedBlockStart.gif {    
 3InBlock.gif    protected void Page_Load(object sender, EventArgs e)
 4ExpandedSubBlockStart.gif    {
 5ExpandedSubBlockEnd.gif    }
 6InBlock.gif
 7InBlock.gif    [ConnectionConsumer("KeyWord","KeywordConsumer")]
 8InBlock.gif    public void GetKeyWord(IKeyWord keyword)
 9ExpandedSubBlockStart.gif    {
10InBlock.gif        ProcessRSSItem(keyword.Message);
11ExpandedSubBlockEnd.gif    }
12InBlock.gif
13InBlock.gif    //解析RSS,并显示解析结果
14InBlock.gif    public void ProcessRSSItem(string keyword)
15ExpandedSubBlockStart.gif    {
16InBlock.gif        System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
17InBlock.gif        rssDoc.Load(Server.MapPath("source.xml"));
18InBlock.gif        System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
19InBlock.gif
20InBlock.gif        string title = "";
21InBlock.gif        string link = "";
22InBlock.gif        string description = "";
23InBlock.gif
24InBlock.gif        for (int i = 0; i < rssItems.Count; i++)
25ExpandedSubBlockStart.gif        {
26InBlock.gif            System.Xml.XmlNode rssDetail;
27InBlock.gif
28InBlock.gif            //读取标题
29InBlock.gif            rssDetail = rssItems.Item(i).SelectSingleNode("title");
30InBlock.gif            if (rssDetail != null)
31InBlock.gif                title = rssDetail.InnerText;
32InBlock.gif            else
33InBlock.gif                title = "";
34InBlock.gif
35InBlock.gif            //如果不包括keyword关键字,则跳转到下一条记录
36InBlock.gif            if (!String.IsNullOrEmpty(keyword) && (title.IndexOf(keyword) == -1))
37InBlock.gif                continue;
38InBlock.gif
39InBlock.gif            //读取URL
40InBlock.gif            rssDetail = rssItems.Item(i).SelectSingleNode("link");
41InBlock.gif            if (rssDetail != null)
42InBlock.gif                link = rssDetail.InnerText;
43InBlock.gif            else
44InBlock.gif                link = "";
45InBlock.gif
46InBlock.gif            //读取描述信息
47InBlock.gif            rssDetail = rssItems.Item(i).SelectSingleNode("description");
48InBlock.gif            if (rssDetail != null)
49InBlock.gif                description = rssDetail.InnerText;
50InBlock.gif            else
51InBlock.gif                description = "";
52InBlock.gif
53InBlock.gif            //输出            
54InBlock.gif            Label label = new Label();
55InBlock.gif            label.Text = "<p><b><a href='" + link + "' target='new'>" + title + "</a></b><br/>" + description + "</p>";
56InBlock.gif            this.Controls.Add(label);
57ExpandedSubBlockEnd.gif        }
58ExpandedSubBlockEnd.gif    }
59ExpandedBlockEnd.gif}

60 None.gif
61 None.gif



4. 在页面*.aspx的WebPartManager中注册提供者和订阅者:

1None.gif<asp:WebPartManager ID="wpManager" runat="server">
2None.gif     <StaticConnections>
3None.gif         <asp:WebPartConnection ID="KeywordConnection"
4None.gif             ProviderID="search" ProviderConnectionPointID="KeyWordProvider"
5None.gif             ConsumerID="content" ConsumerConnectionPointID="KeywordConsumer">
6None.gif         </asp:WebPartConnection>
7None.gif     </StaticConnections>
8None.gif</asp:WebPartManager>

 


附:完整源码下载http://files.cnblogs.com/happyhippy/WebPartDemo.rar

本文转自Silent Void博客园博客,原文链接:http://www.cnblogs.com/happyhippy/archive/2007/04/14/713136.html ,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值