IWebPartParameters makes Connection between Web Parts easy

本文介绍如何利用.NET 2.0中提供的IWebParameters标准接口来简化WebPart之间的通信过程。通过具体示例展示了提供者和消费者模式下接口的实现方法,帮助开发者避免自行设计接口带来的复杂性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

If Google the key words like: web part, connection or communication, we can get a bunch of results. I have to say all articles are the same and the authors talk about how to do it like a professor in the college. The only difference you can find is that the approach to implement and design an interface that makes 2 web parts connects to each other; meanwhile, those guys talk about the benefit we can get if we use the interface. But, do we really necessarily to do it like that? I mean, do we really need to implement or design an interface ourselves?  Too many interfaces make the system hard to understand and maintain. Can we just use an universal one to ease the pain?
Actually, in .NET 2.0, Microsoft providers a standard the IWebParameters to be the communication interface.  However, there’re few people know, I mean in china, how to use it. Even some Chinese MVPs of Microsoft are using the interfaces which they define. They’re all warriors; they have no idea about how much impact the book they wrote can bring to those innocent fresh men. Stop it, stop being buffing and sit down to learn some new things.
As we known, the key of communication of web part is some sort of consumer and provider mode.  And the interface between them is the bridge. I’ll tell you how to use the IWebParameters to be the bridge.
First the provider web part needs to implement the IWebPartParameters:
public class SeachForm : System.Web.UI.WebControls.WebParts.WebPart, IWebPartParameters
//Within the class, you need to some method and property of the IWebPartParameters:

private PropertyDescriptorCollection _PD;
[ConnectionProvider(
"Parameters")]
public IWebPartParameters ConnectionInterface()
{
      
return this;
}


public void GetParametersData(ParametersCallback callback)
{
       IDictionary dic 
= new Hashtable();
       callback.Invoke(dic);
       
//callback.BeginInvoke(dic); 
}


public PropertyDescriptorCollection Schema        
{
            
set { _PD = value; }
            
get return _PD; }
}

public void SetConsumerSchema(PropertyDescriptorCollection schema)
{
            _PD 
= schema;
}

Within the class, you need to implement some method and property of the IWebPartParameters:

public IWebPartParameters ConnectionInterface(). The method would be invoked by Consumer, the purpose of the method is to pass the reference of the IWebParameters to the consumer.

public void GetParametersData(ParametersCallback callback). Key method, the remote method within the consumer would be passed in by the ParametersCallback. And you can invoke the peer method by make some callings to the callback, like following.BTW, all parameters was passes by a standard interface’ IDictionary’ .

public PropertyDescriptorCollection Schema. You have to implement the property, even if you don’t like it, ‘cause it’s part of the interface IWebParameters.

public void SetConsumerSchema(PropertyDescriptorCollection schema). The method would be invoked by consumer. Consumer passes the PropertyDescriptorCollection to the provider by calling the method.

Heres' the consumer :

[ConnectionConsumer("Parameters")]
public void GetConnectionInterface(IWebPartParameters providerPart)
{
    PropertyDescriptor[] property 
= { TypeDescriptor.GetProperties(this)["_sDBAlias"] };
    PropertyDescriptorCollection schema 
= new PropertyDescriptorCollection(property);
    providerPart.SetConsumerSchema(schema);
    ParametersCallback callback 
= new ParametersCallback(ReceiveParameters);
    providerPart.GetParametersData(callback);
}


public void ReceiveParameters(IDictionary Params)
{
  
// Implement all your logic
}

 

And then, all the stories are the same, put 2 web part into the same web page, make they connect to each other in editable mode.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值