Questo esempio di Web Part per SharePoint 2007 di per se non fa molto, prende il valore inserito nel parametro HtmlBody e lo renderizza a video così com'è, quindi puoi inserire anche dell'HTML.

La particolarità dell'esempio è la seconda classe, che permette di definire l'aspetto del pannello di configurazione del parametro HtmlBody. In pratica estendendo dalla classe EditorPart puoi controllare completamente l'aspetto e l'interazione con il pannello di configurazione (vedi anche Open Tool Pane)

Per gestire un pannello custom devi:
  • nella web part (HtmlParametric), settare le proprietà, gestite in modo custom, con l'attributo WebBrowsable(false)
  • sempre nella web part fare l'override del metodo CreateEditorParts ed indicare quali sono le classi che gestiscono i parametri custom (es. HtmlParametricEditor)
  • creare una nuova classe che eredita da System.Web.UI.WebControls.WebParts.EditorPart (HtmlParametricEditor)
  • e fare l'override del metodo ApplyChanges per gestire il salvataggio dei parametri e passarli alla web part
  • fare anche l'override del metodo SyncChanges per gestire il recupero dei parametri dalla web part
  • sempre nella classe di Editor, gestire l'aspetto grafico (metodi CreateChildControls e/o Render) ed eventuali postback
La classe della Web Part
C#
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;

namespace Sgart.it.WP2007
{
  [Guid("CEA33EC7-4F24-49fd-9BB6-7CFB8A78DF71")]
  public class HtmlParametric : System.Web.UI.WebControls.WebParts.WebPart
  {
    private string htmlBody;
    protected LiteralControl lc;

    public HtmlParametric()
    {
      this.ExportMode = WebPartExportMode.All;
    }

    //gestione custom    
    [WebBrowsable(false)]
    [Personalizable(PersonalizationScope.Shared)]
    public string HtmlBody
    {
      get { return this.htmlBody; }
      set { this.htmlBody = value; }
    }

    //questo metodo definisce la classe custom di gestione dei parametri (HtmlParametricEditor)
    public override EditorPartCollection CreateEditorParts()
    {
      List<EditorPart> parts = new List<EditorPart>();

      //la classe che gestisce il pannello di edit custom
      HtmlParametricEditor editor = new HtmlParametricEditor();
      editor.ID = string.Format("{0}_{1}", this.ID, "_HtmlParametric");
      parts.Add(editor);

      return new EditorPartCollection(base.CreateEditorParts(), parts); ;
    }

    protected override void CreateChildControls()
    {
      string s = htmlBody;
      if (string.IsNullOrEmpty(s))
      {
        s = string.Format(@"
Html Parametric by 
<a href=""http://www.sgart.it?prg=HtmlParametric"" target=""_blank"">Sgart.it</a>
<br />
<a id=""HtmlParametricWebPart_OpenToolPane_{0}"" href=""#"" onclick=""javascript:MSOTlPn_ShowToolPane2('Edit','{1}');"">Open the tool pane</a>
to configure this Web Part.", this.ClientID, this.ID);
      }

      lc = new LiteralControl(s);
      this.Controls.Add(lc);

      base.CreateChildControls();
    }

    protected override void Render(HtmlTextWriter writer)
    {
      lc.RenderControl(writer);
    }
  }
}
e la classe custom di editing dei parametri
C#
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace Sgart.it.WP2007
{
  public class HtmlParametricEditor 
    : System.Web.UI.WebControls.WebParts.EditorPart
  {
    protected TextBox txtHtmlBody;

    public HtmlParametricEditor()
    {
      this.Title = "Sgart.it - Html Parametric";
    }

    //salvo i parametri
    public override bool ApplyChanges()
    {
      this.EnsureChildControls();
      HtmlParametric wp = (HtmlParametric)this.WebPartToEdit;
      wp.HtmlBody = txtHtmlBody.Text;
      return true;
    }

    //recupero i parametri salvati
    public override void SyncChanges()
    {
      this.EnsureChildControls();
      HtmlParametric wp = (HtmlParametric)this.WebPartToEdit;
      txtHtmlBody.Text = wp.HtmlBody;
    }

    // gestione custom della visualizzazione
    protected override void CreateChildControls()
    {
      txtHtmlBody = new TextBox();
      txtHtmlBody.ID = "txtHtmlBody";
      txtHtmlBody.TextMode = TextBoxMode.MultiLine;
      txtHtmlBody.Rows = 5;
      this.Controls.Add(txtHtmlBody);

      base.CreateChildControls();
    }

    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
      writer.Write("<div class=\"ms-ToolPartSpacing\"></div>");
      writer.Write("<div class=\"ms-TPBody\">");
      writer.Write("<div class=\"UserSectionHead\">Html</div>");
      writer.Write("<div class=\"UserSectionHead\">");
      txtHtmlBody.RenderControl(writer);
      writer.Write("</div>");

      //writer.Write("<div style=\"width: 100%\" class=\"userdottedline\"></div>");
      writer.Write("</div>");
    }
  }
}
Potrebbe interessarti anche: