How to use User control as web part in share point


It is very easy to develop share point web part using User controls in ASP.net ic .ASCX file. If you use User control for share point development you can save a lot of time. Especially when dealing with design of the web part. I have attached the code for loading a user control please have a look

This has two parts

I. Developing the User control

II. Loading user Control as Web part

First part is easy just use ASP.Net user control for your code

For the part II

1. First you have to put the user control (ASCX file and .CS file of User control)in _Layouts folder of share point .You can even create folder inside _Layouts and put your files there. Another way is you can place the ASCX file in _layouts and the .CS file you can publish as a dll and can place in BIN folder of the application

2. Then Go to Visual studio File->New Project then select Class Library as your project

sharepoint1.gif

3. Then Add share point reference

sharepoint2.gif

4. Add the below name spaces

  • using Microsoft.SharePoint;
  • using System.Web.UI;

  • using Microsoft.SharePoint.WebPartPages;

5. Add the path and name of your user control as shown below.Mine is Demo.ascx and I placed it unde Demo folder in layouts

sharepoint3.gif

6.
Don't forget to strong name if you are going to deploy this web part in GAC .

7. Now you have the webpart that will load the user control.Now you deploy this web part as normal as you do.