Overview
This article gives quick steps for developing web part using web user control (ASCX). Web user controls are designed and developed in web application. Web user control can be loaded into web part, and hence web part development time can be saved by using web user control.
Applies To
Windows SharePoint services
Windows SharePoint Portal Server
Steps
1] Create User Control (ASCX) in web application
2] Create Web Part Project
3] Load User Control in Web Part Project.
- Declare variable _innetcontrol of type Control
- Declare variable strerror of type String
- In Render method write the statement
_innercontrol.RenderControl(writer);
- Write method definition for CreateChildControls
protected override void CreateChildControls()
- Write body for CreateChildControls
this.Controls.Clear ();
innercontrol = this.Page.LoadControl("\\_layouts\\ADVS.ascx");
this.Controls.Add (_innercontrol);
4] Deploy Web Part
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("6f4ba08b-ee02-42d1-af84-bd6467b18509")]
public class UserControl : System.Web.UI.WebControls.WebParts.WebPart{
//Variables
Control _innercontrol;
String strerror;
public UserControl(){
this.ExportMode = WebPartExportMode.All;
}
protected override void Render(HtmlTextWriter writer){
try{
_innercontrol.RenderControl(writer);
}
catch (Exception e){
writer.Write(e.Message + " : " + strerror);
}
}
protected override void CreateChildControls(){
base.CreateChildControls();
try{
this.Controls.Clear();
_innercontrol = this.Page.LoadControl("\\_layouts\\ADVS.ascx");
this.Controls.Add(_innercontrol);
}
catch (Exception e){
strerror = e.Message;
}
}
}
}
Output
