5,286,006 members and growing! (19,624 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » Controls     Advanced License: The Code Project Open License (CPOL)

AjaxConnectedPageViewer

By AdityaBhanu

With Microsoft ASP.NET AJAX 1.0, you can build more dynamic applications that come closer to the rich style of interruption-free interaction. This web part will give full insight of site collection as tree view and respective attributes in as a data grid, which are connected to each other.
C# (C# 2.0, C#), Windows (Windows, Win2003, Vista), .NET (.NET, .NET 3.0, .NET 2.0), Ajax, ASP.NET

Posted: 17 Mar 2008
Updated: 17 Mar 2008
Views: 3,071
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 2.26 Rating: 3.75 out of 5
0 votes, 0.0%
1
1 vote, 25.0%
2
1 vote, 25.0%
3
0 votes, 0.0%
4
2 votes, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This is my second release as a development cycle of AJAX Connectable webpart.While doing some R&D, I found that RowConsumer and Provider interface, ICellConsumer and ICellProvider interfaces are more effective when we are writing the connectable web part in AJAX framework.

Background

This is my second article so for more deep insight of AJAXConnectableWebPart, check out my previous article. AjaxConnectableWebPart_V1.0.0.0

Using the code

All of first we need to derive our webpart from ICellProvider interface.

    /// 
    /// This is an ajax-enabled webpart that sends URL to a connectable PageViewer webpart
    /// 
    public class AjaxUrlListWP : WebPart, ICellProvider
    {
    }
There are some Event required by ICellProvider:
             
//Events required by ICellProvider
        public event CellProviderInitEventHandler CellProviderInit;
        public event CellReadyEventHandler CellReady;

        /// 
        /// This method is called by the Web Part infrastructure to notify the Web Part 
        /// that it has been connected.
        /// 
        /// Friendly name of the interface that is being connected.
        /// Reference to the other Web Part that is being connected to
        /// Friendly name of the interface on the other Web Part through which they are connected
        /// Where the interface can be executed
        public override void PartCommunicationConnect(string interfaceName,
            WebPart connectedPart, string connectedInterfaceName, ConnectionRunAt runAt)
        {
            //Receive connection from "Cell_Provider_Interface_WPQ_" interface only.
            if (interfaceName == "Cell_Provider_Interface_WPQ_")
            {
                //the connection is accepted, and the web part is connected now
                isConnected = true;                
            }
        }

        /// 
        /// This method is called by the Web Part infrastructure to allow the Web Part
        /// to fire any initialization events
        /// 
        public override void PartCommunicationInit()
        {
            //if the web part is connected and the CellProviderInit listener is created,
            //create the args for CellProviderInit event and fire it to tell the Consumer
            //Web Part what type of cell it will be receiving when CellReady is fired later.
            if (isConnected && CellProviderInit != null)
            {
                CellProviderInitEventArgs cellProviderInitArgs = new CellProviderInitEventArgs();
                //set the field name.
                cellProviderInitArgs.FieldName = "URL";
                CellProviderInit(this, cellProviderInitArgs);
            }
        }

        /// 
        /// This is called by the Web Part infrastructure to allow the Web Part to fire any
        /// of the other events from the interface (for example, CellReady). During the
        /// execution of the PartCommunicationMain method, the actual communication of data
        /// values takes place between Web Parts. 
        /// 
        public override void PartCommunicationMain()
        {
            //if the web part is connected and CellReady event is created,
            //create the args for this event and fire it.
            if (isConnected && CellReady != null)
            {
                CellReadyEventArgs cellReadyArgs = new CellReadyEventArgs();
                if (send_data && !string.IsNullOrEmpty(tv.SelectedValue))
                {
                    //set the cell field with the selected URL
                    //this field will be sent to the consumer.
                    cellReadyArgs.Cell = tv.SelectedValue;
                }
                else
                {
                    //nothing was selected
                    cellReadyArgs.Cell = "";
                }
                CellReady(this, cellReadyArgs);
            }
        }

        /// 
        /// Register a client startup script to fixup the update panel, we need this
        /// because Windows SharePoint Services JavaScript has a "form onSubmit wrapper"
        /// which is used to override the default form action.
        /// 
        private void EnsureUpdatePanelFixups()
        {
            if (this.Page.Form != null)
            {
                //modify the form onSubmit wrapper
                if (this.Page.Form.Attributes["onsubmit"] == "return _spFormOnSubmitWrapper();")
                {
                    this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
                }
            }
            //register the script
            ScriptManager.RegisterStartupScript(this, typeof(AjaxUrlListWP), "UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true);
        }
		

We are fetching the sub-sites at run time, when user expand the tree node.So I have written a recursive method, which will take care of filling the treeview at run time.

        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);
                    //GetChild(web1, childnode);
                    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 ICellConsumer Interface and rest of the things are pretty similar to the provider part.

For getting data from Provider part:

        /// 
        /// Implements CellReady Event Handler. Receives data from provider
        /// 
        /// provider web part
        /// arguments sent by the provider
        public void CellReady(object sender, CellReadyEventArgs cellReadyArgs)
        {
            if (cellReadyArgs.Cell != null)
            {
                //gets the URL from the provider and save it in ContentLink
                ContentLink = cellReadyArgs.Cell.ToString();
            }
        }


        #endregion

History

Previous Version - AJAXConnectableWebPart_V1.0.0.0

Current Version - AJAXConnectableWebPart_V1.0.0.1

HAPPY SHAREPOINTING.......Cheers!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

AdityaBhanu


"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.
Occupation: Software Developer (Senior)
Location: India India

Other popular Ajax and Atlas articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralPlease improve formattingmemberMadhur Ahuja8:31 18 Mar '08  
GeneralRe: Please improve formattingmemberAdityaBhanu4:46 16 Jun '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Mar 2008
Editor:
Copyright 2008 by AdityaBhanu
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project