Follow Us

Skip Navigation LinksHome > Articles > Sharepoint > Sharepoint Webpart Using Web User Control

Sharepoint Webpart Using Web User Control

Explains how to create Webpart in sharepoint using Web Server Control

By Project 9X   On   Wednesday, 24 September 2008

Page Views : 69639   |   Technologies : Sharepoint

Rating : Rated :
| More..

Developing Web Part using Web User Control.


In this article we will see how to create web user control (.ascx file) and wrap it inside SharePoint web part. This will significantly save time in developing web part and it helps to resolve child-control-level errors within web part.

Versions supported:

Windows SharePoint services

Windows SharePoint Portal Server

MOSS 2007


Now, we first go with developing web user control (brief) then we’ll move on creating web part.

A. Creating web user control

1) create new web application

2. Add new item > User control

3. Add controls you want in user control on web part.

We have added following code on button click event.

lblMsg.Text = "<b>Details:<b> " + "<br />" +

            "Name: " + txtName.Text + "<br />" +

            "Address:" + txtAddress.Text + "<br />" +

            "City: " + txtCity.Text;  


4. Add user control on aspx page and test it.

5. Output may look like.

 Why UserControl in Web part?

  • Good thing is web part using Usercontrol is easy to create and error on web part will be more specific and displays within web part (Using response.write () method).
  • Moreover you need to change only the GUID and Path of the Usercontrol (.ascx files) if you want to deploy any user control in web part.
  • Compile and Deploy time overhead reduced. Yeah, that’s true. If you want to modify anything in web part just modify aspx page and code in .CS file from layout folder (where we deploy usercontrol files) and u all set. No need to put dll in GAC, bin no IISRESET nothing.   

Now, let’s see how we can plug this user control inside web part of SharePoint.


B. Create Web Part Project

1. Open visual studio 2005

2. Create new project of type ‘Web Part’.

3. Load User Control in Web Part Project.

  • Declare variables

Control _myControl;                                                                   String err;     


Declare variable err of type String

In Render method write the statement




Write method definition for CreateChildControls

protected override void CreateChildControls()


Write body for CreateChildControls

this.Controls.Clear ();

myControl = this.Page.LoadControl("\\_layouts\\WebUserControl.ascx");

this.Controls.Add (_myControl);

4. Deploy Web Part

Note: In latter articles we’ll see how to create and deploy web part and related stuff.

  • Copy WebUserControl.ascx and WebUserControl.ascx.cs files in following directory :

 C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS

  • Copy UserControl.dll from   > Desktop\UserControl\UserControl\obj\Debug

And put it inside BIN directory of your SharePoint site. Drag and drop dll to GAC (Run > c:\windows\assembly)

  • Add <Safecontrol> entry to web.config

<SafeControl Assembly="UserControl, Version=, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" Namespace="UserControl" TypeName="*" Safe="True" />


  • Go to Sitesettings > Modify all Settings > gallery > web part > New and populate new control to gallery.


  • Reset IIS ( Run > cmd > iisreset)
  • New web part is ready to use on any page.


Sample code:

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 UserControl


    [Guid("A1B4DDEA-0CC1-40c8-91B1-339D161E7F45")] // your GUID here.


// Goto tools > create GUID


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




  Control _myControl;

        String err;


        public UserControl()


            this.ExportMode = WebPartExportMode.All;


// Specifies whether all, some, or none of a WebPart control's properties can be exported.



        protected override void Render(HtmlTextWriter writer)






            catch (Exception e)


                writer.Write(e.Message + " : " + err);




        protected override void CreateChildControls()






                _myControl = this.Page.LoadControl("\\_layouts\\WebUserControl.ascx");



            catch (Exception e)


                err = e.Message;






Keywords :
Tags :
Rate This Article :

Comments :

# 1 Annonymous Wrote on 10/02/2008


# 2 Annonymous Wrote on 10/14/2008

hi when i run true your example i get the following error message when adding the webpart to sharepoint "Object reference not set to an instance of an object. : The file '/_layouts/WebUserControl.ascx' does not exist." i followed all the steps , what could be the problem.

# 3 Annonymous Wrote on 10/16/2008

to the previous poster, you should put a ~ (tilde) before your control name. e.g. '/_layouts/WebUserControl.ascx' should actually be '~/_layouts/WebUserControl.ascx' That should fix your problem...

# 4 Annonymous Wrote on 11/14/2008

Hi,can you tell me how do you get to create a web part project.I am working in VS 2005 and it does not show me this option.

# 5 Annonymous Wrote on 11/24/2008

i tried the same way as described but i get webpart error Maintainence Page.. either it says object refernce not set or nothing :-(

# 6 Annonymous Wrote on 11/25/2008

is this applicable in WSS 2.0?

# 7 Annonymous Wrote on 01/08/2009

Nice thing.. It is Working fine.. :-) -- Sathyareka--

# 8 Annonymous Wrote on 02/04/2009

this is fine article. by following this procedure i could create webpart but when i add webpart to the webpartpage it gives me error "file not found"

# 9 Annonymous Wrote on 02/05/2009

very nice

# 10 Annonymous Wrote on 02/23/2009

Object reference not set to an instance of an object. : Could not load type 'WebpartDemo.WebUserControl'.

# 11 Salvodif Wrote on 04/20/2009

Hi, I had the following error: Object reference not set to an instance of an object. :Could not load type 'WebApplication1.WebUserControl1' Any idea?

# 12 Annonymous Wrote on 04/20/2009

Why do you need to copy the .cs file, shouldnt' the code behind be put into the DLL when you compile the User Control? Additonally, if you wanted to make this generic by havingt the URL to the user control as a parameter, would you require different GUIDs for each webpart? Or can you just have a 'wrapper' for the User controls and only require a single WebPart wrapper implementation?

# 13 Annonymous Wrote on 04/21/2009

"Object reference not set to an instance of an object. : Could not load type 'WebpartDemo.xxxx'" - Make sure path is correct "\\_layouts\\WebUserControl.ascx" at this place. or use ~/_layouts/WebUserControl.ascx (or any path that fits in there.) - Make sure your namespace name and class name are right and at right place.

# 14 Annonymous Wrote on 04/21/2009

you tell me how do you get to create a web part project.I am working in VS 2005 and it does not show me this option. -- > Install share point Extension for VS 2005 then you will be able to see those sharepoint options

# 15 Annonymous Wrote on 05/06/2009

RE: "Object reference not set to an instance of an object. : The file '/_layouts/WebUserControl.ascx' does not exist." This only seem to happen while the trust level is not set to "full" in the web.config - not sure what the correct CAS settings are for this to work - continuing to investigate.

# 16 Annonymous Wrote on 06/03/2009

Very nice article

# 17 Annonymous Wrote on 06/26/2009

It's very good code :)

# 18 Annonymous Wrote on 08/20/2009

It is very nice article, Thanks a lot. -- Srikant -- Mumbai.

# 19 Annonymous Wrote on 09/01/2009

how to create toolpart for this webpart using .ascx page

# 20 Annonymous Wrote on 09/30/2009

Nice !!!! Create Custom SharePoint WebParts, it is simple. Try this too,

# 21 Annonymous Wrote on 09/30/2009

To the person who doesn't have the ability to create webparts: You have to install the Visual Studio 200x WSS 3.0 extentions. For VS 2005 the file is called VSeWSS11.exe. For VS 2008 the file is called VSeWSS12.exe. If it isn't obvious you need to have WSS 3.0 or MOSS 2007 installed on your dev box.

# 22 Annonymous Wrote on 11/07/2009

Super , Very Nice Good Work Your's Yogeeswaran

# 23 Annonymous Wrote on 11/24/2009

Thanhs very much

# 24 Annonymous Wrote on 12/24/2009


# 25 Annonymous Wrote on 12/30/2009

Thanks, it worked for me.

# 26 Annonymous Wrote on 12/31/2009


# 27 Annonymous Wrote on 12/31/2009


# 28 Annonymous Wrote on 01/05/2010

Nice Work

# 29 Annonymous Wrote on 01/06/2010

i am getting the 'object reference not set to an instance of an object' error. this is happening inside the 'render' method... has this already been addressed/solved?

# 30 Annonymous Wrote on 01/08/2010

FIX FOR Object reference not set to an instance of an object. : Could not load type 'WebpartDemo.WebUserControl'. Specify the full assembly name in the <%@ Control ... tag of the Inherits property eg. Inherits="WebpartDemo.WebUserControl, WebpartDemo.WebUserControl, Version=, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" Also Usercontrol state is not preserved Good work, have a look at smartpart

# 31 ranger Wrote on 06/04/2010

This is a cool post! Thanks. However, I get the same error which many others seem to have already encountered, was any body able to resolve it? I get the Object reference not set to an instance of an object: Could not load type 'WebpartDemo.WebUserControl'. I have tried replaing the path in the webpart.cs as well. Please let me know if anyone resolved this issue.

# 32 ranger Wrote on 06/04/2010

I am trying to create a webuser controls with a couple of dropsown buttons to capture user's input. On user selections, and hittign of submit button. I need to hit the sql server database and display the results to the user on the Sharepoint site in a repeater or datalist control. Has anyone built a webpart to do this, if so, please can you share your code. Your assistance is greatly appreciated. Thanks.

# 33 vijaymca Wrote on 10/25/2010

I am getting below error "Object reference not set to an instance of an object".. PLease help me..

# 34 vijaymca Wrote on 10/25/2010

While adding Web user control have you checked the 'Place code in Separate file' or not. Please let me know. Because I dont know whether the Sharepoint will support the code in separate page. I was developed by removing the check and I have written the code in the same page.

# 35 rai.anubhav Wrote on 11/12/2010

I am new for Sharepoint . Plz help for me Where to put WebUserControl.ascx.cs when I am deplying Webpart in sharepoint coz I depolyed the webpart sucessfully but I am not getting any data that I am expecting from UserControl.

blog comments powered by Disqus
User Login
Username :
Password :
Register Login

Forgot Password

Related Articles