Click here to Skip to main content
15,878,814 members
Articles / Web Development / ASP.NET
Article

EditorPart over a User Control webpart and moving EditorPart along with a webpart

Rate me:
Please Sign up or sign in to vote.
4.83/5 (15 votes)
17 Jan 20062 min read 87.9K   48   18
How to access User Controls from the EditorPart directly, and how to move EditorParts at runtime.

Image 1

Problem

Standard ASP.NET controls (such as the Textbox and GridView controls), Web User Controls, and even custom controls, can be used as Web Parts. When you treat a standard control as a Web Part, the control is represented in the Web Part framework with the GenericWebPart class.

I have a situation where I have to use user controls as web parts and able to edit them with the custom editor parts, so that I can utilize our organization's existing user controls as web parts. But I was unable to edit them with the custom editor parts, as we can’t access user controls from the editor part directly.

I walked through most of the .NET forums, and I couldn’t find any post about “Creating User control web parts with custom Editor Parts”, so I decided to write a small article to achieve this functionality.

Solution

To implement this functionality, I am using the following:

  1. A Web UserControl
  2. The BaseUserControl class
  3. CustomEditorPart
  4. A Web page

The high level picture of the architecture of the mechanism which I have used to implement the solution, is whown below:

Image 2

BaseUserControl.cs

This is a simple class which inherits System.Web.UI.UserControl. This class will have all the properties which we would like to edit in an EditorPart.

C#
using System;
using System.Data;
using System.Configuration;
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 class BaseUserControl : System.Web.UI.UserControl
{
    private string _myText;
    private string _myColor;
    public BaseUserControl()
    {
        
    }

        public string MyText
    {
        get { return _myText; }
        set { _myText = value; }
    }

        public string MyColor
    {
        get { return _myColor; }
        set { _myColor = value; }
    }
}

WebUserControl.ascx

A simple user control that has a Label control to display messages. At runtime, this user control will be rendered as a generic web part. In order to attach editor parts for this user control, when we click the edit verb, we have to implement the IWebEditable interface.

ASP.NET
<%@ Control Language="C#" AutoEventWireup="true" 
           CodeFile="WelcomeUserControl.ascx.cs" 
           Inherits="WelcomeUserControl" %>
<asp:Label ID="Label1" runat="server" 
       Text="Welcome to India."></asp:Label>

Image 3

WebUserControl.ascx.cs

C#
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;
using System.Drawing;

public partial class WebUserControl : BaseUserControl,IWebEditable
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
        base.MyColor = Label1.ForeColor.ToString();
        
    }
    protected void Page_Prerender(object sender, EventArgs e)
    {
        Label1.ForeColor = Color.FromName(base.MyColor);
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        GenericWebPart gwp = Parent as GenericWebPart;
        if (gwp != null)
        {
            gwp.Title = "Usercontrol webpart2";
        }
    }

    #region IWebEditable Members

    public EditorPartCollection CreateEditorParts()
    {
        ArrayList editorArray = new ArrayList();
        CustomEditorPart edPart = new CustomEditorPart();
        edPart.ID = this.ID + "_customPart1";
        editorArray.Add(edPart);
        EditorPartCollection editorParts =
          new EditorPartCollection(editorArray);

        return editorParts;
    }

    public object WebBrowsableObject
    {
        get { return this; }
    }

    #endregion

}

WelcomeEditorPart.cs

Here is the implementation of the WelcomeEditorPart. This class inherits from EditorPart. The important things in this class are ApplyChanges() which saves the edited value, SyncChanges() which retrieves value from the webpart to edit, and the WebPartToEdit property which returns the currently edited webpart.

C#
using System;
using System.Data;
using System.Configuration;
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 class WelcomeEditorPart : EditorPart
{
    private TextBox _txtWelcome;

    public WelcomeEditorPart()
    {
                this.Title = "Welcome EditorPart";
    }

    public override bool ApplyChanges()
    {
        EnsureChildControls();
        GenericWebPart oPart = (GenericWebPart)WebPartToEdit;
        BaseUserControl control = (BaseUserControl)oPart.ChildControl;
        control.MyText = _txtWelcome.Text;

        return true;
    }

    public override void SyncChanges()
    {
        EnsureChildControls();
        GenericWebPart oPart = (GenericWebPart)WebPartToEdit;
        BaseUserControl control = (BaseUserControl)oPart.ChildControl;
        _txtWelcome.Text = control.MyText;

    }

    protected override void CreateChildControls()
    {
        Controls.Clear();

        _txtWelcome = new TextBox();
        Controls.Add(_txtWelcome);

    }
    protected override void RenderContents(HtmlTextWriter writer)
    {
        writer.Write("Modify text");
        writer.Write(" ");
        _txtWelcome.RenderControl(writer);

    }
}

As we are using GenericWebpart (user controls are runtime rendered as GenericWebpart), first we have to get a reference to GenericWebPart. Then, with GenericWebPart’s ChildControl property, we can access the currently edited webpart (user control).

C#
GenericWebPart oPart = (GenericWebPart)WebPartToEdit;
BaseUserControl control = (BaseUserControl)oPart.ChildControl;
control.MyText = _txtWelcome.Text;

Moving EditorPart along with the WebPart

By default, the editor zone will be displayed at a fixed location wherever we place it. Here is a workaround to move the editor zone to the location of the currently edited webpart. Add the JavaScript below to the Head section of the webpage where we have placed our user controls.

JavaScript
<script language="javascript" type="text/javascript">
function MoveEditorPart()
{
    if(document.getElementById('EditorZone1')!= null)
    {
        var control = document.getElementById('Hidden1').value;
        var oTr = document.getElementById('WebPart_'+control).insertRow(1);
        var oTd = oTr.insertCell();
        oTd.appendChild(document.getElementById('EditorZone1'));
    }
}
</script>

Sample design layout of the web page:

Image 4

Default.aspx.cs

C#
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode;
        WebPartZone1.HeaderText = " ";
        WebPartZone2.HeaderText = " ";
       
    }

    protected void Page_Prerender(object sender, EventArgs e)
    {
        if (WebPartManager1.SelectedWebPart != null)
        {
            Hidden1.Value = WebPartManager1.SelectedWebPart.ID;
        }
    }
}

Things that can be added to improve

  • Implementing personalization.

Sample Screens

Image 5

Image 6

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionI had some clarification regarding this article Pin
vignesh9572-May-14 18:20
vignesh9572-May-14 18:20 
GeneralThis is what I was looking for Pin
r_maiya17-Feb-09 12:54
r_maiya17-Feb-09 12:54 
QuestionData not being saved Pin
Sarojanand Jha12-Aug-08 11:49
Sarojanand Jha12-Aug-08 11:49 
AnswerRe: Data not being saved Pin
GeirO17-Nov-08 0:45
GeirO17-Nov-08 0:45 
GeneralRe: Data not being saved Pin
radeks14-Nov-11 6:55
radeks14-Nov-11 6:55 
Generaldatabase to get values Pin
stanz911-Nov-07 6:55
stanz911-Nov-07 6:55 
how / where would i put code to grab value of welcome text from db?
GeneralSharepoint portal Pin
Prakash A 19804-Oct-07 21:44
Prakash A 19804-Oct-07 21:44 
QuestionHow to get drag and drop to work in Firefox Pin
Big Dog22-Aug-07 8:43
Big Dog22-Aug-07 8:43 
GeneralThank you very much, Mr. Chary Pin
Big Dog22-Aug-07 8:23
Big Dog22-Aug-07 8:23 
General&amp;amp;#35874;&amp;amp;#35874; THS U [modified] Pin
XuShiHao31-May-07 0:16
XuShiHao31-May-07 0:16 
GeneralThanks for your code Pin
Jos Branders29-Jan-07 4:24
Jos Branders29-Jan-07 4:24 
Generalgood Pin
paofu14-Dec-06 22:54
paofu14-Dec-06 22:54 
QuestionCannot get it to work Pin
j.channon24-May-06 1:50
j.channon24-May-06 1:50 
GeneralThanks Pin
samuel.hs.lo20-Apr-06 1:03
samuel.hs.lo20-Apr-06 1:03 
GeneralThere was an Exception Pin
walid100113-Mar-06 19:19
walid100113-Mar-06 19:19 
GeneralRe: There was an Exception Pin
kbio15-Sep-06 0:02
kbio15-Sep-06 0:02 
Generalout of date Pin
kisoft28-Jan-06 11:59
kisoft28-Jan-06 11:59 
GeneralRe: out of date Pin
V Sridhar Chary31-Jan-06 22:59
V Sridhar Chary31-Jan-06 22:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.