Click here to Skip to main content
15,896,448 members
Articles / Programming Languages / C#

WebParticles: Developing and Using Web User Controls as WebParts in Microsoft Office SharePoint Server 2007

Rate me:
Please Sign up or sign in to vote.
4.90/5 (61 votes)
27 Mar 200714 min read 512.6K   1.1K   165  
Using Web User Controls in SharePoint WebParts
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
/*
    the conditional compilation section allows 
    development
    of sharepoint parts inside ASP.NET 
*/
using Microsoft.SharePoint.WebPartPages;

namespace SmartParticles
{
    public class WebParticle: WebPart
    {
        private Control _control;
        private string _exceptions = "";
#if SHAREPOINT
        private string _userControlPath = @"~/usercontrols/";
#else
        private string _userControlPath = "";
#endif
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            
            try
            {
                string tooldir = this.Page.Request.MapPath(_userControlPath);
                string[] textArray1 = null;
                try
                {
                    textArray1 = Directory.GetFiles(tooldir);
                }
                catch (Exception exception1)
                {
                    _exceptions +=  string.Format("Could not open user control path: {0}", exception1.ToString());
                    return;
                }
                _control = this.Page.LoadControl(_userControlPath + @"webparticlecontrol.ascx");

            }
            catch (Exception CreateChildControls_Exception)
            {
                _exceptions += "CreateChildControls_Exception2: " + CreateChildControls_Exception.Message;

            }//end catch

        }//end protected override void CreateChildControls()

        protected override void RenderContents(HtmlTextWriter writer)
        {
            try
            {
                base.RenderContents(writer);
                _control.RenderControl(writer);
            }
            catch (Exception RenderContents_Exception)
            {
                _exceptions += "RenderContents_Exception: " + RenderContents_Exception.Message;
            }
            finally
            {
                if (_exceptions.Length > 0)
                {
                    writer.WriteLine(_exceptions);
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect Dell
United States United States
I am a Big Data Architect working with MPP Database Systems and Hadoop.

Comments and Discussions