Click here to Skip to main content
6,635,160 members and growing! (17,527 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

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

By V Sridhar Chary

How to access User Controls from the EditorPart directly, and how to move EditorParts at runtime.
C#, Javascript, Windows, .NET 2.0, ASP.NET, ADO.NET, WebForms, VS2005, Dev
Posted:17 Jan 2006
Views:47,043
Bookmarked:43 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 5.29 Rating: 4.75 out of 5

1

2
1 vote, 7.7%
3
2 votes, 15.4%
4
10 votes, 76.9%
5

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:

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.

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.

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

WebUserControl.ascx.cs

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.

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).

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.

<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:

Default.aspx.cs

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

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

About the Author

V Sridhar Chary


Member

Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
GeneralThis is what I was looking for Pinmemberr_maiya13:54 17 Feb '09  
QuestionData not being saved PinmemberSaroj_Itoolpro12:49 12 Aug '08  
AnswerRe: Data not being saved PinmemberGeirO1:45 17 Nov '08  
Generaldatabase to get values Pinmemberstanz97:55 11 Nov '07  
GeneralSharepoint portal PinmemberBrave_Gass22:44 4 Oct '07  
GeneralHow to get drag and drop to work in Firefox PinmemberBig Dog9:43 22 Aug '07  
GeneralThank you very much, Mr. Chary PinmemberBig Dog9:23 22 Aug '07  
General&amp;#35874;&amp;#35874; THS U [modified] PinmemberXuShiHao1:16 31 May '07  
GeneralThanks for your code PinmemberJos Branders5:24 29 Jan '07  
Generalgood Pinmemberpaofu23:54 14 Dec '06  
QuestionCannot get it to work Pinmemberj.channon2:50 24 May '06  
GeneralThanks Pinmembersamuel.hs.lo2:03 20 Apr '06  
GeneralThere was an Exception Pinmemberwalid100120:19 13 Mar '06  
GeneralRe: There was an Exception Pinmemberkbio1:02 15 Sep '06  
Generalout of date Pinmemberkisoft12:59 28 Jan '06  
GeneralRe: out of date PinmemberV Sridhar Chary23:59 31 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Jan 2006
Editor: Smitha Vijayan
Copyright 2006 by V Sridhar Chary
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project