Adding and Deploying a User Control in a WebPart with VSeWSS

Here we will add an existing User Control as a part of our WebPart Soultion package (that we created using VSeWSS webpart template), so that the control gets deployed in _controltemplates folder along with the solution deployment proccess.

Also, we will add the .dll of UserControl in GAC via our manifest.xml file.

To add an Existing User Control in a WebPart, Created using VseWss, see the instructions below.
Note : We will deploy our User Control in _controlsTemplate and will load it dynamically in the webpart.

Creating User Control:

1. Create a user control in a Web application project.
2. Sign the project assembly.

Creating a WebPart Project.

1. Create a new WebPart Project. ( You need Vsewss Installed for this).

2. Write the below code. The CreateChildControls here will load the User control from _Controltemplates ( the location where we will deploy our control) when the webpart renders.

Webpart Code :


namespace PVH.PhoneDirectory.WebPart
{

[Guid("48ef83e6-440b-4465-8401-2852ea3fb8b1")]

public class PhoneDirectorySearchWebPart : System.Web.UI.WebControls.WebParts.WebPart
{

UserControl userControl;

public PhoneDirectorySearchWebPart()
{

this.ExportMode = WebPartExportMode.All;

}

public override string Title
{
get
{
return "WebPart With Control";
}
set
{
base.Title = value;
}
}

protected override void Render(HtmlTextWriter writer)
{
userControl.RenderControl(writer);
}

protected override void CreateChildControls()
{

base.CreateChildControls();

userControl = (UserControl)Page.LoadControl(@"/_controltemplates/CustomWebPart /UserControl.ascx");

this.Controls.Add(userControl);

}}}

3. Now, create a Templates folder struture in your webpart. The structure would be
Templates->ControlTempates->CustomWebPart.

4. Add your control (.ascx file) and your controls .dll (the signed assembly of the Web application project) under CustomWebPart Folder, so that It gets deployed in controls Folder.



Note : Please note that we have not added the code file for the control in the Custom WebPart's Folder.


5. Now before building and deploying this solution, Lets change the ascx file of the control to Inherit from the .dll of the Control. This will allow your ascx file to access the code file of the control from .dll itself.

The Changes are :


<%@ Control Language="C#" AutoEventWireup="true" Inherits="ControlNameSpace.ControlClass, ControlNameSpace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2ef8d0c4bab8980b" Debug="true" %>

If you want to deploy other files(like Css or js) along with your webpart, you can do it by craeting the same directory stucture as we created for _controlstemplate
See example :
Adding .css or .js files in 12 hive

6. Finally, we need to add the User Control's .dll into GAC and also, add an entry for our User control into the SafeControls.

So, Open the manifest file and add another assembly under Assemblies tag. Also, Add a Safe Control entry below that.(see the Image Below)

Note : The Location for the controls .dll is from our webParts Template Structure.



Now Build the Project and Deploy the Soulton.

You will see that your manifest.xml file will have the entries for our control under Template tag.

1 comments:

  1. Hi,unless of-course you decide to use one of the many templates available on-line which in turn help you design a web page in Web Design Cochin that is not so good and the least creative.Thanks..........

    ReplyDelete

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com