Knowledge Walls
Gopal Rao
Mumbai, Maharashtra, India
Passcode:
Custom editorpart class sharepoint example
3849 Views
Introduction 
In this example i have made one text box in tool part to give the search url with editor part class.
Web part class code
	# region References
	using System;
	using System.Collections.Generic;
	using System.Text;
	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 Microsoft.SharePoint;
	using System.ComponentModel;
	using System.Xml;
	using System.Xml.Serialization;
	using Microsoft.SharePoint.WebPartPages;
	#endregion
	
	namespace google_editor_part
	{
	   public class googlePart : System.Web.UI.WebControls.WebParts.WebPart
	   {
	       #region EditorProperties
	       private string searchUrl;
	       #endregion
	       private TextBox txtField;
	       private Button btnSearch;
	       #region Properties
	       [Personalizable(PersonalizationScope.Shared), WebBrowsable(false)]
	       public string SearchUrl
	       {
	           get
	           {
	               return searchUrl;
	           }
	           set
	           {
	               searchUrl = value;
	           }
	       }
	       #endregion
	       #region Editor Part
	       /// <summary>
	       /// The editor properties will be created for the web part
	       /// </summary>
	       ///
	        public override EditorPartCollection CreateEditorParts()
	       {
	           List<EditorPart> objEditorParts = new List<EditorPart>(1);
	           EditorPart objeditorpart = new googleEdit();
	           objeditorpart.ID = this.ID + "Editor";
	           objEditorParts.Add(objeditorpart);
	           EditorPartCollection objBasePart = base.CreateEditorParts();
	           return new EditorPartCollection(objBasePart, objEditorParts);
	       }
	       #endregion
	       /// <summary>
	       /// Override the CreateChildControls method to add the User Control
	       /// </summary>
	       #region CreateChildControls
	       protected override void CreateChildControls()
	       {
	           txtField = new TextBox();
	           Controls.Add(txtField);
	           btnSearch = new Button();
	           btnSearch.Click +=new EventHandler(btnSearch_Click);
	           btnSearch.Text = "click";
	           Controls.Add(btnSearch);
	       }
	       #endregion
	       protected void btnSearch_Click(object sender,EventArgs e)
	       {
	           string url = searchUrl + txtField.Text;
	           Context.Response.Redirect(url);
	       }
	       protected override void RenderContents(HtmlTextWriter writer)
	       {
	           txtField.RenderControl(writer);
	           btnSearch.RenderControl(writer);
	       }
	   }
	}
Editor part class code
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.Web;
	using System.Web.UI;
	using Microsoft.SharePoint;
	using System.Web.UI.WebControls;
	using System.Web.UI.WebControls.WebParts;
	
	namespace google_editor_part
	{
	   public class googleEdit : EditorPart
	   {
	       #region Members
	       TextBox strSearch;
	       #endregion
	
	       #region Create Child Controls
	       /// <summary>
	       /// Creates the custom properties in the editor part
	       /// </summary>
	       ///
	       protected override void CreateChildControls()
	       {
	           base.CreateChildControls();
	           strSearch = new TextBox();
	           Controls.Add(new LiteralControl("<div><label>Write the URL:</label><br />"));
	           Controls.Add(strSearch);
	           Controls.Add(new LiteralControl("</div>"));
	       }
	       #endregion
	
	       #region Apply Changes
	       /// <summary>
	       /// This will apply the changes made by the user to the editor properties
	       /// </summary>
	       /// <returns></returns>
	       ///
	        public override bool ApplyChanges()
	       {
	           try
	           {
	               this.EnsureChildControls();
	               googlePart objWebPart = (googlePart)this.WebPartToEdit;
	               objWebPart.SearchUrl = strSearch.Text;
	           }
	           catch (Exception objEx)
	           {
	               throw objEx;
	           }
	           return true;
	       }
	       #endregion
	       #region Sync Changes
	       /// <summary>
	       /// This will synchronize the edited custom properties as web part properties
	       /// </summary>        
	       ///
	       public override void SyncChanges()
	       {
	           try
	           {
	               this.EnsureChildControls();
	               googlePart objWebPart = (googlePart)this.WebPartToEdit;
	               strSearch.Text = objWebPart.SearchUrl;
	           }
	           catch (Exception objEx)
	           {
	               throw objEx;
	           }
	       }
	       #endregion
	   }
	}
Previous Topics
Previous lessons of current book.
Best Lessons of "Microsoft sharepoint"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details