Tuesday, February 3, 2009

Developing and Using Web User Control in Web Parts in MOSS 2007


1) Open a new ASP.Net Web Site and name it appropriately.



2) Add a Web User Control file and name it appropriately.



3) Design and create controls according to your need in ascx file.



4) Write the code at button click events as per your requirements.



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserEntry : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string strName = txtFirstName.Text + " " + txtLastName.Text;
string strAddress = txtCity.Text + ", " + ddlState.SelectedItem.Text + ", " + txtCountry.Text;

lblResName.Text = strName;
lblResAddress.Text = strAddress;
}
protected void btnReset_Click(object sender, EventArgs e)
{
txtFirstName.Text = "";
txtLastName.Text = "";
txtCity.Text = "";
ddlState.SelectedIndex = 0;
txtCountry.Text = "";
}
}





5) Copy your both ascx and ascx.cs file in following path:

C:\Inetpub\wwwroot\wss\VirtualDirectories\80\UserControls

If UserControls folder is not present then create it.

6) Now open a new Web Control Library project and name it appropriately. Add reference of Microsoft.SharePoint.dll in your project. It is present at following path:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll



7) Open a Web Custom Control and name it properly.



8) Inherit Web Part class from System.Web.UI.WebControls.WebParts.WebPart.
Override the CreateChildControls method to load the previously created ascx Web Control.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

namespace UserControl
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:SPUserControlWP runat=server>")]
public class SPUserControlWP : WebPart
{
private Control _control;

protected override void CreateChildControls()
{
_control = this.Page.LoadControl(@"~/usercontrols/UserEntry.ascx");
this.Controls.Add(_control);
}
}
}



9) Create Strong-Name for project and build it.

10) Write following codes in your site web.config (i.e. C:\Inetpub\wwwroot\wss\VirtualDirectories\80\web.config) file.




Namespace="UserControl" TypeName="*" Safe="True" />











Note: For details regarding way to create strong name and copy version and PublicTokenKey, refer my earlier post.

11) Create webpart file for Web Part.








Cannot import this Web Part.





UserControl Web Part


A
demonstration using UserControl in a SharePoint
WebPart








12) Add the above created *.webpart xml file to your site.


Note: For details regarding the way to add Web Part, please refer my earlier post --- Developing Custom Web Part.


Suggested Link:

1) WebParticles: Developing and Using Web User Controls as WebParts in Microsoft Office SharePoint Server 2007
http://www.codeproject.com/spoint/WebParticles.asp

No comments:

Post a Comment