Developing and Using Web User Control in Web Parts in MOSS 2007
 
1) Open a new ASP.Net Web Site and name it appropriately. 
 
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
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></{0}:SPUserControlWP>")]
    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.
<SafeControls>
  <SafeControl Assembly="UserControl, Version=1.0.2721.26545,
            Culture=neutral, PublicKeyToken=1af8fa800ec59b02"
            Namespace="UserControl" TypeName="*" Safe="True" />
</SafeControls>
<assemblies>
  <add assembly="UserControl, Version=1.0.2721.26545, 
            Culture=neutral, PublicKeyToken=1af8fa800ec59b02" />
</assemblies>
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.
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="UserControl.SPUserControlWP" />
      <importErrorMessage>
        Cannot import this Web Part.
      </importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">
          UserControl Web Part
        </property>
        <property name="Description" type="string">
          A
          demonstration using UserControl in a SharePoint
          WebPart
        </property>
      </properties>
    </data>
  </webPart>
</webParts>
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



 































_525_526_513.gif) 
 
 Posts
Posts
 
