In order to add a webpart into the module implement in the class ModuleInterface overloading of method GetWebPartInfos(), that returns collection WPInfoList with definition of your webparts. Number of webparts is not limited.
Create a child class of the class WPInfo for each webpart and insert it into the collection. This child is responsible for creation of the webpart instance and control element for webpart configuration.
public class ModuleInterface : ModuleInterfaceBase
{
...
public override WPInfoList GetWebPartInfos()
{
WPInfoList l = new WPInfoList();
l.Add( new MenuWPInfo(this));
return l;
}
}
public class MenuWPInfo : WPInfo
{
public readonly Guid gwpMenu = new Guid("{ ENTER A NEW GUID FOR WEBPART }");
private ModuleInterface module;
public MenuWPInfo(ModuleInterface module) : base( new MenuWebPart())
{
this.module = module;
InfoId = gwpMenu;
}
public override WebPartBase GetWebPartInstance(ContentPage cp, WPBase wp, bool isDesign)
{
return new MenuWebPart(wp, cp, isDesign, module);
}
public override EditWebPart GetWebPartEditControl()
{
return new MenuEditControl();
}
}
When creating a new child of class of WPInfo do not forget to define a new unique ID (type guid).
Function GetWebPartInstance returns a new webpart instance. This instance will be inserted into the page.
Function GetWebPartEditControl returns an instance of the control for webpart editing.
Create a class for the webpart itself now.
public class MenuWebPart : WebPartBase
{
public ModuleInterface Module { get; private set; }
public MenuWebPart() : base()
{ }
public MenuWebPart(WPBase webPart, ContentPage contentPage, bool isWPDesign, ModuleInterface module)
: base(webPart, contentPage, isWPDesign)
{
this.Module = module;
}
public override string Title
{
get { return "cs-CZ::Webpart Menu~en-US::Webpart Menu"; }
}
public override string Description
{
get { return "cs-CZ::Nazev~en-US::Name"; }
}
public override string TitleImageUrl
{
get { return "./Modules/" + Module.SubDirectory + "/ImagesData/menu.png"; }
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//initialize
//my_property = (WebPart == null ? null : WebPart.Text1);
if (IsWpSet())
{
Page.ClientScript.RegisterClientScriptInclude("Module_Menu", "./modules/OG.menu/Module_Menu.js");
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
try
{
if (IsWpSet())
{
// your code...
//insert css file
//Page.Header.Controls.Add(new LiteralControl("
quot;);
}
}
catch (Exception ex)
{
Module.Application.OG.Log.WriteException(ex, Module);
RenderError();
}
}
Properties Title, Description and TitleImageUrl displayed in the dialogue window for selection of a new webpart for the page.
Method RenderError writes a standard error message in the page.
Prtoperty Module.SubDirectory contains name of the folder in which the module is stored (see code of the property TitleImageUrl). we recommend to store modules in particular folders in order they would be well separated.
Webpart editing
If the webpart is more complex, you may need to create a control element for its configuration.
public class MenuEditControl : EditWebPart
{
private TextItem ti;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
EnsureChildControls();
}
protected override void CreateChildControls()
{
base.CreateChildControls();
OGRow ro = new OGRow();
ro.ID = "ro";
Controls.Add(ro);
OGCol co = new OGCol() { ID = "cc1", OneColumn = true };
ro.Controls.Add(co);
TitleItem2 titleItem = new TitleItem2();
titleItem.ID = "titi";
titleItem.Text = "Definice menu";
ti = new TextItem();
ti.ID = "texi";
ti.Rows = 10;
OGItem ic = new OGItem(titleItem, ti) { ID = "item" };
co.Controls.Add(ic);
}
public override void LoadWebPart(WPBase wp)
{
EnsureChildControls();
ti.SetData(wp.Text1);
}
public override void PreSaveWebPart(WPBase wp)
{
EnsureChildControls();
wp.Text1 = ti.GetDataAsString();
}
public virtual void SaveWebPart(WPBase wp)
{
}
public override string GetHelpUrl()
{
return "http://www.objectgears.cz";
}
}
Create controls for editing in the method CreateChildControls.
Method LoadWebPart is used for filling in controls with date from the webpart. If you store webpart data in your custom table, read the data according to the webpart ID here.
Before saving you can save the data from controls into the webpart (variable wp) in the method PreSaveWebPart. You can also save the data in a custom table in the method SaveWebPart.
Function GetHelpUrl returns URL to documentation for the webpart. The documentation is available to users by clicking on the question mark icon in the upper right corner.
Data saving
Webpart configuration data can be saved directly into the webparta data instance (WPBase) or into your custom table. If you use your custom table you have to read the data, update and delete them your self. You can use webpart ID as a link to the webpart.
In case that you do not need any special properties for the configuration, you can use following properties in the webpart.
Dato type |
Property |
Description |
string |
Text1
Text2
Text3
Text4
Text5
Text6 |
Text property. |
bool |
Bool1
Bool2
Bool3 |
Indication Yes/No. |
int |
Int1
Int2
Int3 |
Integer. |
DateTime? |
DateTime1
DateTime2
DateTime3 |
Date (nullable). |
In order to avoid work with your custom table you can save your configuration into xml and use e.g. property Text1.
Webpart deletion
If you use webpart properties for saving webpart configuration, you do not need to care about deletion. However, if you use your custom table, then it is necessary to implement in the class ModuleInterface methods DeleteWebPartInstance and DeleteWebPartInstances. The first method is called during deletion of a single webpart, the second one during deletion of the whole page.
Therefore, it is necessary to save webpart ID and page ID into your custom table if you use it.