Using Custom Web Part Editor

You saw in the previous section that you can create your own custom properties that can appear in the Web Part Editor pane. This works for simple properties, but if you have more complex properties, you will want to create your own custom Web Part Editor. You will see that you not only can create a custom Web Part Editor, but you can make this editor be a Silverlight Editor.

Start by adding a new class called SLEditorPart under the SLWebPart folder in your SLWebPart project. Add the code in Listing 6-11 to the SLEditorPart.cs file.

Listing 6-11: Using statements for the custom Silverlight Web Part Editor

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.SilverlightControls;

namespace SLWebPart {

public class SLEditorPart:EditorPart {

//the ID of the editor part has to be unique for // each instance of your webpart.

public SLEditorPart(string webPartID) {

this.ID = "SLEditorPart" + webPartID;

protected override void OnLoad(EventArgs e) {

base.OnLoad(e); #region ScriptManager

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);

scriptManager = new ScriptManager(); Controls.AddAt(0, scriptManager);

#endregion

protected override void CreateChildControls() {

base.CreateChildControls();

Silverlight ctrl = new Silverlight(); ctrl.ID = "SLWebPartID";

ctrl.Source = "~/XAP_Bin/SilverlightControl.xap"; ctrl.Width = Unit.Percentage(100); Controls.Add(ctrl);

public override void SyncChanges()

public override bool ApplyChanges() {

SLWebPart part = (SLWebPart)WebPartToEdit; part.Title = "Updated"; return true;

Imports System

Imports System.Collections.Generic Imports System.Linq Imports System.Text

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.WebControls.WebParts

Imports System.Web.UI.SilverlightControls Imports System.Web.UI.HtmlControls

Public Class SLEditorPart Inherits EditorPart Public Rating As HiddenField Private RatingValue As String = "1"

'the ID of the editor part has to be unique for each instance of your webpart. Public Sub New(ByVal webPartID As String)

Me.ID = "SLEditorPart" & webPartID End Sub

Protected Overrides Sub OnLoad(ByVal e As EventArgs) MyBase.OnLoad(e)

'#Region "ScriptManager"

Dim scriptManager As ScriptManager = scriptManager.GetCurrent(Me.Page) If scriptManager Is Nothing Then scriptManager = New ScriptManager() Controls.AddAt(0, scriptManager) End If '#End Region End Sub

Protected Overrides Sub CreateChildControls() MyBase.CreateChildControls()

Continued

Listing 6-11: Using statements for the custom Silverlight Web Part Editor (continued)

'TODO: rEMOVE AFTER test 'Rating = 3;

'hidden field to hold the value from the Silverlight slider control

Rating = New HiddenField()

Rating.ID = "RatingId"

Rating.Value = RatingValue

MyBase.Controls.Add(Rating)

Dim ctrl As New Silverlight() ctrl.ID = "SLWebPartID"

ctrl.Source = " ~/XAP_Bin/SilverlightControl.xap" ctrl.Width = Unit.Pixel(230) ctrl.Height = Unit.Pixel(300) 'comma delimited key=value pairs ctrl.InitParameters = String.Format("DisplayMode={0},RatingControlId={1}", "Editor", Rating.ClientID)

Controls.Add(ctrl) End Sub

'get values from Web Part Public Overrides Sub SyncChanges()

'SLWebPart part = (SLWebPart)WebPartToEdit; 'RatingValue = part.RatingXX.ToString(); End Sub

'Update Web Part values

Public Overrides Function ApplyChanges() As Boolean 'SLWebPart part = (SLWebPart)WebPartToEdit; 'part.Rating = Rating; Return True End Function End Class

The code in Listing 6-11 inherits from the System.Web.UI.WebControls.WebParts.EditorPart. This is very similar to the standard Web Part class except that it has two additional methods — SyncChanges and ApplyChanges. In the same way that .NET properties use a special variable called value to pass the Property value to the set method of a property, the SyncChanges and ApplyChanges use the WebPartToEdit variable.

□ SyncChanges — This is called by SharePoint when your Editor part is loaded. The method is used to pass data from the Web Part being edited to the Editor part. Use this method to update properties in the Editor part.

□ ApplyChanges — This method is called when you apply the changes from the Web Part Editor. This is triggered by the user clicking on the Apply button at the bottom of the Editor pane. You can see in Listing 6-11 that the ApplyChanges sets the title property of the Web Part being edited. You will see in the Data Connectivity chapter (Chapter 7) how to connect these two methods with the real data from the Silverlight control.

The only other change from a standard Web Part is the fact that the editor must have a unique ID. One technique to generate a unique ID is to pass the ID of the Web Part being edited to the constructor of the Editor part.

0 -1

Average user rating: 1 stars out of 1 votes

Post a comment

  • Receive news updates via email from this site