Tuesday, November 03, 2009

Visual Web Part is the new Smart Part

Visual Studio 2010 comes with a template for VisualWebPart.

It creates a web part (.cs), User Control (.ASCX) and other associated files.

The Web Part maintains a reference to the User Control and loads the same in the CreateChildControls method.

private const string _ascxPath = @"~/_CONTROLTEMPLATES/ContosoWebParts/FirstWebPart/MyUserControl.ascx";

protected override void CreateChildControls()
  Control control = this.Page.LoadControl(_ascxPath);

The actual functionality can be developed in the Web User Control (MyUserControl.ascx) which can leverage the Visual Studio Designer

The approach is similar to Return of Smart Part for MOSS 2007, which we used to wrap ASCX file within an web part.

1 comment:

Vinay Bhatia said...

SharePoint 2010 is also AJAX enabled by default. No manual updates to the web.config are required.
Add a reference to System.Web.Extensions. In the Visual Studio Toolbox, find the AJAX Extensions tab, and drag drop the UpdatePanel control onto the user control designer