Adding user control to SharePoint 2007 (MOSS/WSS) Web Part and handling events in there

View the updated one: Part 2 - Adding user control to SharePoint 2007 (MOSS/WSS) Web Part and handling events in there - Step by Step with task pane properties.

 

Requirement: I have a Web User Control where I added one textbox and a button. Now I wanted to add this user control into a SharePoint Web Part. After adding this Web Part to any page, if I click the button, there should be a text “Hello World” in the Text Box.

Steps:

  1. Create a blank web application and add a web user control (name WebUserControl.ascx) into it. While adding the user control, uncheck the "Place code in a separate file" option.
  2. Add one TextField and a Button to the user control. The ids of these controls are TextBox1 and Button1 respectively.
  3. Now copy this user control to a folder (wuc) under your virtual host (Web Application) path. In my case it is: “C:\Inetpub\wwwroot\wss\VirtualDirectories\80\wuc\”.
  4. Now create a web part application using Visual Studio SharePoint Web Part template. Here is the code for the web part:

using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace wucwp

{

    [Guid("ee818646-e872-455c-a857-6e64e513539b")]

    public class wucwp : System.Web.UI.WebControls.WebParts.WebPart

    {

        UserControl userControl;

        System.Web.UI.WebControls.TextBox txtField;

        System.Web.UI.WebControls.Button showButton;

        public wucwp()

        {

            this.ExportMode = WebPartExportMode.All;

        }

        protected override void Render(HtmlTextWriter writer)

        {

            // TODO: add custom rendering code here.

            // writer.Write("Output HTML");

            userControl.RenderControl(writer);

        }

        protected override void CreateChildControls()

        {

            base.CreateChildControls();

            userControl = (UserControl) Page.LoadControl(@"/wuc/WebUserControl.ascx");

            txtField = (System.Web.UI.WebControls.TextBox) this.userControl.FindControl("TextBox1");

            showButton = (System.Web.UI.WebControls.Button) this.userControl.FindControl("Button1");

            showButton.Click += new EventHandler(showButton_Click);

            Controls.Add(userControl);

        }

        void showButton_Click(object sender, EventArgs e)

        {

            //throw new Exception("The method or operation is not implemented.");

            txtField.Text = "Hello World";

        }

    }

}

Now deploy this web part and add it to any page of your site and this should work as expected.

 

 

Update Note: One minor modification. It is better to put the .WebUserControl.ascx file under 12-Hive\TEMPLATE\CONTROLTEMPLATES folder, and access it as userControl = (UserControl) Page.LoadControl(@"/_controltemplates/WebUserControl.ascx");

 

Thanks Inge for your inputs.