This is a member of the Webpart base class. The method is called before the Web Part renders. This method provides notification to connectable Web Part that it should register its interfaces by using the RegisterInterface method. EnsureInterface method should be definitely overridden when developing connectable Web Parts. The RegisterInterface method occurs within the EnsureInterface method. ///<summary>
///EnsureInterfaces
///----Notification to the Web Part that is
should ensure that all
///----its interfaces are registered using RegisterInterface.
///</summary>
///
publicoverridevoidEnsureInterfaces()
{
try
{
//Register the ICellProvider interface
RegisterInterface("Ex1CellConsumerInterface_WPQ_ ",
//InterfaceName: Unique name of
the interface
" ICellConsumer ",
//InterfaceType: The type of the
interface
WebPart.UnlimitedConnections,
//MaxConnections: sets how many
connections can
//be formed on this interface
ConnectionRunAt.Server,
//RunAtOptions: where the
interface can be run
this,
//InterfaceObject: reference to
the actual
//object
that implements this interface
" CellConsumerClientInterface_WPQ_ _",
//InterfaceClientReference: for client-side connections,
//a
string that is used as the identifier for the client-
//side
object that implements this interface
"
Cell Consumer ",
//MenuLabel: a general label for
the interface
//(used in Authoring Connections)
" Consumes a
string from a Text Box ");
//Description: an extended explanation of the
//interface
(used in Authoring Connections)
}
catch(SecurityException e)
{
//Display an error message if the Interface will
//not register properly
interfaceRegistrationError = true;
}
}
Figure 5 - EnsureInterface and RegisterInterface methods. There is a possibility that code access security may prevent the interface being registered if the Web Part is installed in the bin directory and not contain proper trust levels enabled in web.config file. It is recommended to use try/catch block to handle any problems may occur when registering an interface.
RegisterInterface method takes following parameters.
InterfaceName
This is a friendly name of the interface. It should be a string property with unique value.
InterfaceType
The interface type. (Ex: ICellProvider, ICellConsumer, etc)
MaxConnections
Sets how many connections can be formed on this interface. Developer can select either LimitOneConnection allowing single connection or UnlimitedConnections allowing maximum connection limit.
RunAtOption
Where the interface can be run. (Ex: Client, Server, ClientAndServer, None)
InterfaceObject
Reference to the actual object that implements the interface.
InterfaceClientReference
This is a string value that will be used as the identifier for the client side object, which implements that interface.
MenuLable
The value that the developer enters here will appear in the connection menu user interface. It is a good practice to mention whether this is a Provider or Consumer Web Part in this section to indicate the direction of the dataflow.
Description
A description about the interface.
There is one more parameter supported by RegisterInterface method. The allowCrossPageConnection parameter indicates whether this interface supports the cross page connections. By default, this parameter is set to True for all connection interfaces supported by the connection compatibility rules.