IWebPartParameters makes Connection between Web Parts easy---csdn陈亮

http://blog.csdn.net/savage54321/archive/2008/01/14/2043216.aspx 

 

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.

posted @ 2008-08-28 23:42  彷徨......  阅读(1297)  评论(0编辑  收藏  举报