Introduction
This is my second release as a development cycle of an AJAX Connectable webpart. While doing some R&D, I found that the RowConsumer and Provider interfaces, ICellConsumer
and ICellProvider
are more effective when we are writing a connectable web part in the AJAX framework.
Background
This is my second article, so for a more deep insight of AJAXConnectableWebPart
, check out my previous article: AjaxConnectableWebPart_V1.0.0.0.
Using the code
First, we need to derive our web part from the ICellProvider
interface:
public class AjaxUrlListWP : WebPart, ICellProvider
{
}
There are some events required by ICellProvider
:
public event CellProviderInitEventHandler CellProviderInit;
public event CellReadyEventHandler CellReady;
public override void PartCommunicationConnect(string interfaceName,
WebPart connectedPart, string connectedInterfaceName, ConnectionRunAt runAt)
{
if (interfaceName == "Cell_Provider_Interface_WPQ_")
{
isConnected = true;
}
}
public override void PartCommunicationInit()
{
if (isConnected && CellProviderInit != null)
{
CellProviderInitEventArgs cellProviderInitArgs = new CellProviderInitEventArgs();
cellProviderInitArgs.FieldName = "URL";
CellProviderInit(this, cellProviderInitArgs);
}
}
public override void PartCommunicationMain()
{
if (isConnected && CellReady != null)
{
CellReadyEventArgs cellReadyArgs = new CellReadyEventArgs();
if (send_data && !string.IsNullOrEmpty(tv.SelectedValue))
{
cellReadyArgs.Cell = tv.SelectedValue;
}
else
{
cellReadyArgs.Cell = "";
}
CellReady(this, cellReadyArgs);
}
}
private void EnsureUpdatePanelFixups()
{
if (this.Page.Form != null)
{
if (this.Page.Form.Attributes["onsubmit"] == "return _spFormOnSubmitWrapper();")
{
this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
}
}
ScriptManager.RegisterStartupScript(this, typeof(AjaxUrlListWP), "UpdatePanelFixup",
"_spOriginalFormAction = document.forms[0].action;" +
" _spSuppressFormOnSubmitWrapper=true;", true);
}
We are fetching the sub-sites at runtime, when the user expands the tree node. So, I have written a recursive method which will take care of filling the treeview at runtime.
public void CreateTreeOnExpandNode(string URL, TreeNode nodeExpanded)
{
try
{
SPSite site = new SPSite(URL);
SPWeb web = null;
if (site.Url == URL)
{
web = site.OpenWeb();
}
else
{
URL = URL.Replace(site.Url, "");
if (site.ServerRelativeUrl != "/") URL = site.ServerRelativeUrl + URL;
web = site.OpenWeb(URL);
}
foreach (SPWeb web1 in web.Webs)
{
TreeNode childnode = new TreeNode(web1.Url);
if (web1.Webs.Count > 0)
{
TreeNode emptyNode = new TreeNode();
childnode.ChildNodes.Add(emptyNode);
}
nodeExpanded.ChildNodes.Add(childnode);
}
}
catch (Exception ex)
{
Page.Response.Write(ex.Message);
}
}
Consumer part: For creating the consumer part, I have derived my control class from the ICellConsumer
interface, and the rest of the things are pretty similar to the provider part.
For getting data from the provider part:
public void CellReady(object sender, CellReadyEventArgs cellReadyArgs)
{
if (cellReadyArgs.Cell != null)
{
ContentLink = cellReadyArgs.Cell.ToString();
}
}
#endregion
Happy SharePointing! Cheers!!!
History
"In the attitude of silence the soul finds the path in an clearer light, and what is elusive and deceptive resolves itself into crystal clearness. My life is a long and arduous quest after knowledge & Truth."
I'm playing with all ancient and new Microsoft Technologies since last two and half years.