Integrating
“User control “in web part tool editor (Editor Panel):
Code: Display user control in Web part Tool part (Editor
Panel).
* Author: Madhu Nomula.
STEP1: create web
part which inherits web parts, IWebEditable(fore editor part)
* Declare the variables those will be
accesed by the Editor tool pane.
public string _Name { get; set; }
public string _Country { get; set; }
STEP2: Add user
control with required controls.
Create a folder named "Control
Templates" in the solution and
add a new User Control(Farm Solution
only) in that folder (NMC_UserControl.ascx)
* If we use the ddl, it is hard to capture the
selected value in the UC. To capture that I have used Hidden field
* <asp:HiddenField runat="server"
ID="hfddlCountrydetectRequest" Value="0" />
STEP3: create a CS
file(InheritUserControl.cs) which inherits EditorPart class.
* Call the instances of web part and user
control in this CS and pass the parameters in both ways.
STEP4: In web part
CS file create new instance of CS(which was created in STEP3) and add this to
List<EditorPart>
_______________________________________________________________________________________
Step1: Create
Solution and add web part(“VisualWebPart1”) using VS 2013
Step2: Create
folder named “ControlTemplates” and add the user control “NMC_UserControl”
* If we use the ddl, it is hard to
capture the selected value in the UC. to capture that I have used Hidden field
* <asp:HiddenField runat="server"
ID="hfddlCountrydetectRequest" Value="0" />
**************************************************************
NMC_UserControl.ascx
<asp:HiddenField runat="server" ID="hfddlCountrydetectRequest" Value="0" /><table><tr><td>Name</td><td>
<asp:TextBox ID="txtName"runat="server"></asp:TextBox></td></tr>
<tr><td>Country</td><td>
<asp:DropDownList ID="ddlCountry" runat="server">
</asp:DropDownList></td></tr></table>
NMC_UserControl.ascx.cs
namespace
NMC_UserControl_WPEditorPanel.ControlTemplates.NMC_UserControl_WPEditorPanel
{
public partial class NMC_UserControl : UserControl
{
public string varName { get; set; }
public string varCountry { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//
Code to Check if this is the first Page_Load of the User control to make sure
DDL loads only once
if (this.hfddlCountrydetectRequest.Value == "0")
{
this.hfddlCountrydetectRequest.Value = "1";//This code to make sure for next time page load it
will not load the ddl controls again.
ddlCountry.Items.Add("India");
ddlCountry.Items.Add("USA");
ddlCountry.Items.Add("UK");
ListItem li = new ListItem("Select
Country", "Select Country");
ddlCountry.Items.Insert(0, li);
}
if (!string.IsNullOrEmpty(varCountry))
{
ddlCountry.SelectedValue =
varCountry;
}
else
{
varCountry =
ddlCountry.SelectedItem.Value;
}
if (!string.IsNullOrEmpty(varName ))
{
txtName.Text = varName.Trim();
}
else
{
varName = txtName.Text.Trim();
}
}
}
}
STEP3: create a
CS(InheritUserControl.cs) file which
inherits EditorPart class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace NMC_UserControl_WPEditorPanel.VisualWebPart1
{
class InheritUserControl:EditorPart
{
public InheritUserControl()
{
this.Title = "Custom
Properties";
}
//Declaring
variable to load usercontrol location from Server location 15 hive
const string UserControlConfigurationPath = @"~/_CONTROLTEMPLATES/15/NMC_UserControl_WPEditorPanel/NMC_UserControl.ascx";
//Declaring
the object of User control
ControlTemplates
.NMC_UserControl_WPEditorPanel .NMC_UserControl
NMC_Usercontrol;
//Declaring
the object of Webpart
NMC_UserControl_WPEditorPanel.VisualWebPart1.VisualWebPart1
NMC_CustomWebpart;
protected override void CreateChildControls()
{
//
Get a reference to the Edit Tool Pane.
ToolPane pane = this.Zone as ToolPane;
if (pane != null)
{
//
Disable the validation on Cancel Button of ToolPane
pane.Cancel.CausesValidation = false;
//
pane.Cancel.Click += Cancel_Click;
}
//
Load the User Control and it to the Controls Collection of the Editor Part
this.NMC_Usercontrol =
this.Page.LoadControl(UserControlConfigurationPath) as
ControlTemplates.NMC_UserControl_WPEditorPanel.NMC_UserControl;
this.Controls.Add(NMC_Usercontrol);
}
//Apply Changeswill execute when click Apply/Ok button
public override bool ApplyChanges()
{
EnsureChildControls();
//Code
to call the properties of Webpart and assign from User control
NMC_CustomWebpart = this.WebPartToEdit as VisualWebPart1 ;
if (NMC_CustomWebpart != null)
{
NMC_CustomWebpart._Name =
NMC_Usercontrol.varName ;
NMC_CustomWebpart._Country =
NMC_Usercontrol.varCountry;
NMC_CustomWebpart.SaveChanges();
}
return true;
}
//SyncChanges will execute when click OK
public override void SyncChanges()
{
EnsureChildControls();
NMC_CustomWebpart = this.WebPartToEdit
as VisualWebPart1;
if (NMC_CustomWebpart != null)
{
NMC_Usercontrol.varName = NMC_CustomWebpart._Name ;
NMC_Usercontrol.varCountry = NMC_CustomWebpart._Country;
}
}
}
}
Step4:
/*
Code Sample : Display user control in Webpart
Tool part(Editor Panel).
* Author: Madhu Nomula.
*
STEP1:
create webpart which inherits webparts,IWebEditable(fore editor part)
* Declare the variables those will be accesed
by the Editor tool pane.
public string _Name { get; set; }
public string _Country { get; set; }
STEP2:
Add usercontrol with required controls.
Create
a floder named "Control Templates" in the solution and
add
a new User Control(Farm Solution only) in that folder (NMC_UserControl.ascx)
* If we use the ddl, it is hard to capture the
selected value in the UC. to capture that I have used Hiddenfield
* <asp:HiddenField runat="server"
ID="hfddlCountrydetectRequest" Value="0" />
STEP3:
create a CS file(InheritUserControl.cs) which inherits EditorPart class.
* Call the instances of webpart and
usercontrol in this CS and pass the parameters in both ways.
STEP4:
In webpart CS file create new instance of CS(which was created in STEP3) and
add this to List<EditorPart>
*/
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace NMC_UserControl_WPEditorPanel.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public class VisualWebPart1 : WebPart, IWebEditable
{
// Visual Studio might
automatically update this path when you change the Visual Web Part project
item.
private const string _ascxPath = @"~/_CONTROLTEMPLATES/15/NMC_UserControl_WPEditorPanel/VisualWebPart1/VisualWebPart1UserControl.ascx";
protected override void CreateChildControls()
{
Control control =
Page.LoadControl(_ascxPath);
Controls.Add(control);
}
public string _Name { get; set; }
public string _Country { get; set; }
public void SaveChanges()
{
this.SetPersonalizationDirty();
}
EditorPartCollection IWebEditable.CreateEditorParts()
{
//Creting the object of
existing Editor part
List<EditorPart> editorParts = new List<EditorPart>();
//creating the object of Custom CS
file(InheritUserControl.cs)
InheritUserControl newEditor = new InheritUserControl();
newEditor.ID = this.ID + "_CustomUserControlEditorPart";
newEditor.Title = "Custom
Properties";
//Adding the User control to
existing editor part
editorParts.Add(newEditor);
return new EditorPartCollection(base.CreateEditorParts(),
editorParts);
}
protected override void Render(HtmlTextWriter writer)
{
//base.Render(writer);
writer.Write("Name:" +
_Name+"<br>");
writer.Write("Country:" +
_Country);
}
}
}
No comments:
Post a Comment