代码改变世界

Web parts 通信

2008-08-21 10:36  Johnson2008  阅读(387)  评论(0编辑  收藏  举报

1. 定义通信接口


public interface IEmployeeInfo
{
    
int EmployeeID { getset; }
}

2. 在提供者中定义接口成员

 

    public class EmployeesWebPart : WebPart, IEmployeeInfo
    {
        [Personalizable(
true), WebBrowsable(true)]
        
public int EmployeeID
        {
            
get { return _empID; }
            
set { _empID = value; }
        }
    
//
    }

3. 在提供者中实现[ConnectionProvider]方法

 

    public class EmployeesWebPart : WebPart, IEmployeeInfo
    
{
        [Personalizable(
true), WebBrowsable(true)]
        
public int EmployeeID
        
{
            
get return _empID; }
            
set { _empID = value; }
        }

        
//

        
// 定义ProvideEmployeeInfo方法,传送连接数据
        [ConnectionProvider("EmployeeIDProvider""EmployeeIDProvider")]
        
public IEmployeeInfo ProvideEmployeeInfo()
        
{
            
return this;
        }


        
//
    }

4. 在订阅者中实现[ConnectionConsumer]方法

    public class OrdersWebPart : WebPart
    
{
        
public OrdersWebPart()
        
{
            
this.Title = "通信订阅者";
        }


        
private int _empID;
        
private GridView gv = new GridView();
        
private AccessDataSource ads;

        
protected override void RenderContents(HtmlTextWriter writer)
        
{
            
if (_empID < 1)
            
{
                writer.Write(
"<span style='color:gray'>请在文本框中输入大于等于1的值.</span>");
                
return;
            }

            
else
            
{
                ads 
= new AccessDataSource();
                Controls.Add(ads);
                ads.ID 
= "accessDatasource1";
                ads.DataFile 
= "~/App_Data/NorthWind.mdb";
                ads.SelectCommand 
= "SELECT top 8 orderid, customerid,employeeid, orderdate from orders where employeeid=" + _empID.ToString();

                gv 
= new GridView();
                Controls.Add(gv);
                gv.DataSourceID 
= "accessDatasource1";
                gv.CellPadding 
= 3;
                gv.GridLines 
= GridLines.None;
                gv.RenderControl(writer);
            }

        }


        [ConnectionConsumer(
"EmployeeIDConsumer""EmployeeIDConsumer")]
        
public void GetEmployeeInfo(IEmployeeInfo empInfo)
        
{
            
if (empInfo != null)
                _empID 
= empInfo.EmployeeID;
            
else
                
throw new NotSupportedException("未能连接数据库");
        }

    }

 

5. 在WebPartManager控件中声明连接.

<table border="0">
                    
<tr>
                        
<td colspan="2">
                            
<asp:WebPartManager ID="WebPartManager1" runat="server">
                                
<StaticConnections>
                                    
<asp:WebPartConnection ID="EmpConnection" ProviderID="EmployeesWebPart1" ProviderConnectionPointID="EmployeeIDProvider"
                                        ConsumerID
="OrdersWebPart1" ConsumerConnectionPointID="EmployeeIDConsumer" />
                                
</StaticConnections>
                            
</asp:WebPartManager>
                        
</td>
                    
</tr>
                    
<tr valign="top">
                        
<td>
                            
<asp:WebPartZone ID="WebPartZone1" runat="server" Width="300px">
                            
<ZoneTemplate>
                                
<cw1:EmployeesWebPart ID="EmployeesWebPart1" runat="server" EmployeeID="1" />
                            
</ZoneTemplate>
                            
</asp:WebPartZone> </td>
                        
<td>
                            
<asp:WebPartZone ID="WebPartZone2" runat="server" Width="300px">
                                
<ZoneTemplate>
                                    
<cw1:OrdersWebPart ID="OrdersWebPart1" runat="server" />
                                
</ZoneTemplate>
                            
</asp:WebPartZone>
                            
</td>
                    
</tr>
                
</table>