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.




  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



    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”);




        protected override void 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);




        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.

Comments (30)

  1. Satish V says:


    I am new to sharepoint.. Nice and useful example.

    Rather than handling events in the webpart. Cant we handle events in WebUserControl.ascx.cs and keep in a dll file?

  2. aashita says:


    i am nesting the  2 usercontrol. but when i try to access textbox.text value, first time it retrun me blank value and 2nd time returning me the actual value. Can u guide why this problem occuring.


    ctrlUser = (UserControl)this.Page.LoadControl(strControlPath1);

                       GoalControl =(UserControl) this.Page.LoadControl(strControlPath2);



    if (ViewState["Obj"].ToString() == "Internal")


                   if (ViewState["Obj"].ToString() == "Finance")


                   if (ViewState["Obj"].ToString() == "Org")


    txtObj = PnlCustomer.Controls[0].FindControl("txtObj") as TextBox;

    but this txtObj, first time returning blank value. Since i am switiching the GoalControl to different Placeholder during run time.

  3. suhail says:

    This helps me only for path, but good example

  4. This works fine but now I’m to the point of creating connections between controls like this and I’m wondering if there are any best practices or pointers.  I’m doing the majority of the work in the inherited class for the user control – data binding, handling control events etc.  The web part is pretty dumb.  It’s really just a host for the user control.  I’m assuming I can’t simply define Connection Consumer / Connection Provider methods inside of the user control (unless the web part class has some sort of mechanism for searching it’s sub controls for connection points).  So what do a do?  Do I expose some methods from my user control that the web part can use to satisfy the connection?  For example, the user control has a grid view that is bound to data that it retrieves internally (the web part class knows nothing about it).  But now I want to expose that data for a connection.

  5. Salvo Di Fazio says:


    I would like to know how can I deploy my webpart with webusercontrols by the packeg created with the Sharepoint WebPart Project


  6. Abhishek verma says:

    Hi Pranab,

              It was such a really nice article which i was looking for. Pranab, It is good when we have one o r two usercontrols but, how can we associate almost 15 usercontrols into it and make easy navigation among all user controls in a single web part. One more that I would like to take in your consideration that if we go this way then it would be hard to handle the events of all user controls on single web part.

           Kindly advise me. I have to implement 15 user controls into single web part.

    Thanks & Regards,

    Abhishek Verma

    New Delhi


  7. Since my post “ Adding user control to SharePoint 2007 (MOSS/WSS) Web Part and handling events in there

  8. Since my post “ Adding user control to SharePoint 2007 (MOSS/WSS) Web Part and handling events in there

  9. Raju says:

    the events of the controls present in the webpart are not firig in MOSS as i am attached the events in OnInit please tell us how to fire the events.

  10. Ray Ng says:

    In the ASP.NET world, database connection strings are typically stored in the web.config of the application.  This doesn’t seem to be the case

    with SharePoint. Can anybody tell me how this scenario is handled in SharePoint?

    Your help will be appreciated.

Skip to main content