Hi,
in the last post of my post series about Visual Web Part development we saw how we can build Editor Parts for our Web Parts. The main advantage of using Visual Web Parts is the ability of using the Visual Studio 2010 designer to build the Web Part. Why not using the same idea to build Visual Editor Parts?
In this post, I show you in simple steps how to create your first Visual Editor Parts. Our example will only show how to prepare a Visual Editor Part without logic and controls. However, if you know how to do it with classic Editor Parts, it should not be any issue.
Step 1: create a test Visual Web Part project and create a new Visual Web Part with the name VisualEditorPartExample (there are several posts in my post series explaining how to do it). You should see something like that:

Step 2: Add a new user control and call it EditorUserControl.ascx
Step 3: The user control is placed automatically in the ControlTemplates:

Step 4: We want to put the EditorUserControl.ascx in the same container as our Visual Web Part (in our case VisualEditorPartExample). Simply drag & drop the user control EditorUserControl.ascx in the VisualEditorPartExample folder. It will look like next picture:

Step 5: To finish this configuration we are going to change the Deplyoment location path property of the EditorUserControl.ascx. Currently, the path value is composed by {ProjectName}\{Visual Web Part Name}. However, we want to change it to reflect the same path of our Visual Web Part. To do this, we only need to add the CONTROLTEMPLATES folder in front of our path. In our case we change the property from Lamber.TestPart\VisualEditorPartExample\ to CONTROLTEMPLATES\Lamber.TestPart\VisualEditorPartExample\. You can see the property change in the next figure:
Until now, we only added an user control to the project and configured it to be deployed in the same folder of our Visual Web Part. This user control will be used to create the visual logic for our Editor Part. We do normally the same with our Visual Web Parts. However, we did not create any Editor Parts yet. Let us do it immediately…
Step 6: Add a new class VisualEditorPartExample container and call it EditorPartExampleEditor.cs. This will be our old school Editor Part controlling the storage of properties with the synch and apply methods. In addition, this class is responsible to load the user control that we created before. The code of the Editor Part may look like in the next code snipped:
namespace Lamber.TestPart.VisualEditorPartExample
{
class EditorPartExampleEditor : EditorPart
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @"~/_CONTROLTEMPLATES/Lamber.TestPart/VisualEditorPartExample/EditorUserControl.ascx";
protected override void CreateChildControls()
{
Control control = Page.LoadControl(_ascxPath);
Controls.Add(control);
}
public override bool ApplyChanges()
{
// put the applychanges code here
return true;
}
public override void SynchChanges()
{
// put the synchchanges code here
}
}
}
The important lines lie between 6-12, where we are going to load the user control EditorUserControl.ascx in our Editor Part. You see, that this is the same logic used by the Visual Web Parts. Please note that the example above adds no logic to this Editor Part.
Step 7: Finally, we have only to add the Editor Part reference to our EditorPartCollection of our Visual Web Part. Change the code of the VisualEditorPartExample.cs like in the code snipped below:
public override EditorPartCollection CreateEditorParts()
{
EditorPartExampleEditor editor = new EditorPartExampleEditor();
editor.ID = this.ID + = "_example";
List<EditorPart> collection = new List<EditorPart>();
collection.Add(editor)
return new EditorPartCollection(collection);
}
Summary
That’s all, deploy the project and test your freshly created Visual Editor Part. Naturally, you have to add the logic and connections with the personalization properties. You might find hints how to do it in my last post. However, I think that the idea is somehow clear and easy to follow.
Happy coding,
Patrick