This is a member of the Webpart base class. The method is called before the Web Part renders. This method notifies the 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>
///
publicoverridevoid EnsureInterfaces()
{
try
{
//Register the ICellProvider
interface
RegisterInterface("Ex1CellProviderInterface_WPQ_",
//InterfaceName:
Unique name of the interface
"ICellProvider",
//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
"CellProviderClientInterface_WPQ_",
//InterfaceClientReference:
for client-side connections,
//a string
that is used as the identifier for the client-side object that implements this
interface
"Provider a single value
from a Text Box",
//MenuLabel: a general label for the
interface
//(used in Authoring
Connections)
"Pass the value entered in to the Text Box to the Consumer
Web Part");
//Description: an extended explanation of the
//interface (used in Authoring
Connections)
}
catch(SecurityException e)
{
//Display an
error message if the Interface won’t register properly
interfaceRegistrationError = true;
}
}
Figure 6 - EnsureInterface and RegisterInterface methods.
There is a possibility that Code Access Security may prevent registration to the interface if the Web Part is installed in the bin directory and does not contain proper trust levels that are enabled in the web.config file. It is recommended to use a try/catch block to handle any problems that may occur when registering an interface.
In the above code, I am providing an interface name to the Web Part. The ICellProvider Web Part will run only on the server side supporting unlimited connections. I have entered a value to the client side interface reference even if I don't use it in my exercise. The developer has the option to enter an empty string value for the client side reference if he is not using it. The menu Label text will display on the menu bar of the Web Part when it is on the design view. It will explain the functionality of the Web Part. In catch section, I am using a Boolean value to indicate whether the interface registration was successful.
RegisterInterface method takes following parameters.
InterfaceName
This is the friendly name of the interface. It should be a string property with a unique value.
InterfaceType
The interface type. (Ex: ICellProvider, ICellConsumer, ECT)
MaxConnections
Sets how many connections can be formed on this interface. The 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 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 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 that are supported by the connection compatibility rules.