程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> IWebPartParameters makes Connection between Web Parts easy

IWebPartParameters makes Connection between Web Parts easy

編輯:.NET實例教程
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.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved