The above code sample will determine whether it's connected, number of connected Web Parts, and the name of the connected Web Part.
Step 6 - Override the PartCommunicationInit Method
This method is called when the Web Part needs to fire any initialization events. The Web Part will fire any event ending with "Init". For example: CellProviderInit. The Init parameters are used when a Web Part needs to provide information about itself to other Web Parts. The developer has the option to override this method.
///<summary>
/// PartCommunicationInit
///----At this
time, a part should fire any events that end with 'Init'
///----if they can.
///----##
ICellProvider Example: fire "CellProviderInit"
///</summary>
///
publicoverridevoid PartCommunicationInit()
{
//Is it connected
if(webpartConnected)
{
//If there is a listener, send
init event
if (CellProviderInit != null)
{
//Need to create the args for
the CellProviderInit event
CellProviderInitEventArgs cellProviderInitArgs = new
//This basically tells the
Consumer Web Part what type of
//cell it will be receiving
when CellReady is fired
later.
CellProviderInit(this,
cellProviderInitArgs);
}
}
}
Figure 9 - PartCommunicationInit method
Step 7 - Override the PartCommunicationMain Method
This method passes the primary data to other connected Web Parts. This allows the Web Part to fire any other events from the interface, such as the CellReady event. The developer has the option to override this method.
///<summary>
/// PartCommunicationMain
///----At this
time, a part can fire any events that it wants to.
///----## ICellProvider Example: fire "CellReady"
///</summary>
///
publicoverridevoid PartCommunicationMain()
{
//Is it connected
if(webpartConnected)
{
//If there is a listener, send
CellReady event
if (CellReady != null)
{
//Need to create the args for
the CellProviderInit event
CellReadyEventArgs cellReadyArgs = new
CellReadyEventArgs();
//Set the Cell to the value of
the Textbox text
//This is the value that will
be sent to the Consumer
cellReadyArgs.Cell = InputBox.Text;
//Fire the CellReady event.
//The Consumer will then
receive the Cell value
CellReady(this,
cellReadyArgs);
}
}
}
Figure 10 - PartCommunicationMain method
The above code passes the string value in the text box to the Web Part it's connected to.
Step 8 - Override the RenderWebPart Method
RenderWebPart method is overridden by the webpart base class. This method is called by the Web Part architecture to render the HTML for the body of the Web Part.
///<summary>
/// Render this
control to the output parameter specified.
///</summary>
///<param
name="output"> The HTML writer to write out to </param>
//Make sure that all necessary child
controls are created
EnsureChildControls();
//If an error occurs while trying to
register the interface
if(interfaceRegistrationError)
{
//There is no interfaces
connected.
output.Write(" <FONT COLOR='RED'>An error has
occured
while
trying to register the interface.</FONT>");
//Create a break
output.RenderBeginTag(HtmlTextWriterTag.Br);
output.RenderEndTag();
}
else
{
//If connected
if(intNumberOfConnections
> 0)
{
//Create a header
output.RenderBeginTag(HtmlTextWriterTag.H4);
//Write
the header test
output.Write("Web Part is connected on Server
Side
to
");
output.Write(connectedWebPartName);
output.RenderEndTag();
//Render the TextBox
InputBox.Enabled = true;
output.Write("Enter the text");
InputBox.RenderControl(output);
//Create a break
output.RenderBeginTag(HtmlTextWriterTag.Br);
output.RenderEndTag();
//Render the button
SubmitButton.Enabled = true;
SubmitButton.RenderControl(output);
}
else
{
//There is no interfaces
connected.
output.Write(" <FONT COLOR='RED'>No Cell
Consumer
Connected.Please
select a Cell Consumer to enable
the
Text Box and the Button.</FONT>");
//Create
a break
output.RenderBeginTag(HtmlTextWriterTag.Br);
output.RenderEndTag();
output.Write("Enter the text");
//Render the TextBox
InputBox.RenderControl(output);
//Create a break
output.RenderBeginTag(HtmlTextWriterTag.Br);
output.RenderEndTag();
//Render the button
SubmitButton.RenderControl(output);
}
}
}
Figure 11 - RenderWebPart method
The code is looking for any error that occurs during the interface registration before rendering any controls. An error message will display if an error has occurred. Otherwise, if the Web Part is connected, the controls will be rendered with the text box and button controls. The interface controls will be disabled at the first instance until the Web Part gets connected. When the Web Part is connected to a compatible consumer Web Part, the end user will be able to enter a value to the text box and click the button.
Step 9 - Override the CreateChildControls Method
This method creates all the user interface controls necessary for the Web Part, in this example, the text box and the button controls.
///<summary>
/// Creates all the
user interface controls necessary for the Web Part
///</summary>
protectedoverridevoid CreateChildControls()
{
//Create The Text Bob
InputBox = new TextBox();
InputBox.ID = "InputBox";
InputBox.Enabled = false;
//Add to the Control List
Controls.Add(InputBox);
//Create the button
SubmitButton = new Button();
SubmitButton.ID = "SubmitButton";
SubmitButton.Text = "Submit";
SubmitButton.Enabled= false;
//Add to the control list
Controls.Add(SubmitButton);
SubmitButton.Click += new EventHandler(SubmitButtonClick);
}
Figure 12 - CreateChildControls method
Upload ICellProvider Web Part
Navigate to the SharePoint Web page's top right hand corner and select Modify My Page or Modify Shared Page, depending on whether the end user is in Personal or Shared view. Select Add Web Part | Import option. Browse to the "CellProviderWebPart.dwp" file and click Upload button. This will upload the Cell Provider Web Part as displayed below:
Figure 13 - Upload the Cell Provider Web Part
Drag and drop the "Cell Provider Web Part" to the SharePoint page as displayed below.
Figure 14 - Cell Provider Web Part
The above Web Part indicates it's not connected to a consumer Web Part. The end user has the option to connect this provider Web Part with a compatible consumer Web Part any time. (Please refer to the article "Connectable Web Parts in SharePoint portal 2003" for more information regarding how to connect a Web Part. (.
Figure 15 displays the Cell Provider Web Part connected to a Cell Consumer Web Part.
Figure 15 - The Cell Provider Web Part is connected to a Cell Consumer Web Part
It displays the title of the connected Web Part in the header section. In this case, it is connected to Cell Consumer Web Part. The Cell Consumer Web Part will consume the value entered in the text box.
About the Author
Gayan Peiris is a Senior Consultant for Unique World Pty Ltd (www.uniqueworld.net) in Canberra, Australia. He is a MCSD with MCAD, MCP, Bcom and MIT certifications. Gayan has designed and developed Microsoft Web and Windows solutions since 1999. His expertise lies in developing scalable, high-performance applications with Microsoft Enterprise Server Products in .NET technology .His core skills are ADO.NET, ASP.NET, C#, VB.NET, Web Services, XML, SharePoint Portals, DNA and SQL Server.
This sample chapter from Packt Publishing's "Building Websites with the ASP.NET Community Starter Kit"
illustrates how to build a new module on top of the existing code in the ASP.NET Community Starter Kit (CSK).
Using a Frequently Asked Questions (FAQ) module as an example, it shows how creating a new module allows you
to add entirely new features which integrate seamlessly with the rest of the framework. [Read This Article][Top]
Dino Esposito discusses the differences between the DataGrid control in
version 1.x and 2.0 of ASP.NET. In the process, he also builds an improved
version of the 1.x control that can get you some of the new 2.0 features
today. [Read This Article][Top]
Most default SharePoint Server Web Parts can be connected across organizations. The second article in this series shows how
to develop connectable Web Parts that provide information to other Web Parts. [Read This Article][Top]
Alex Homer continues to highlight some of the new ASP.NET 2.0 accessibility features. These features make it easier for visually impaired users to view and navigate Web sites and provide better support for alternative types of browsers and user agents. [Read This Article][Top]
Most default SharePoint Server Web Parts can be connected across organizations. The first article in this series explains how to connect existing Web Parts using the connection Interface classes in the SharePoint architecture. [Read This Article][Top]
Alex Homer highlights some of the new ASP.NET 2.0 accessibility features. These features make it easier for visually impaired users to view and navigate Web sites and provide better support for alternative types of browsers and user
agents. [Read This Article][Top]
Alex Homer discusses the simplification of, and extensions to, the ASP.NET 1.x data binding syntax, the new two-way data binding syntax for updating data sources, and the new syntax for binding to XML data in ASP.NET 2.0. [Read This Article][Top]
This article examines some of the new and exciting caching features in ASP.NET 2.0 and shows how to implement them in Web applications. [Read This Article][Top]
When implementing custom components that require access to restricted resources, implicit impersonation must be used. Jay Nathan shows how to create a class that makes using .NET Impersonation a snap. [Read This Article][Top]
Jeff Gonzalez demonstrates how to create a flexible configuration section
handler using C#. He provides a summary background of the .NET
configuration system, explains why the system is useful, and shows how it can be extended. [Read This Article][Top]
Mailing List Want to receive email when the next article is published? Just Click Here to sign up.