kamagra how much to take

Static Versioning in a WebPart EditorPart

Where you put WebPart versioning is something that is up to debate, discussed previously here.

Today a client asked me if it was feasible to construct a custom EditorPart control to display such information in a reasonable manner, so that versioning data could be shown only when modifying the WebPart on the web surface. The code to do this is pretty simplistic, and one can hydrate the versioning either ala Reflection or if desired by using static string literals. Doesn’t really matter.

The first thing is a static method using an HtmlTextWriter consuming two string values representing the version of the WebPart and the friendly name of the product. This could be reflected on within this method if the parameters being abstracted is desired, or supplied when the method is called. I’m just going to use string literals because this is just an example.

  1. public class VersionInfo
  2. {
  3. public static void CreateVersionSegement(HtmlTextWriter writer, string version, string name)
  4. {
  5. var tbl = new Table { HorizontalAlign = HorizontalAlign.Center };
  6. var row = new TableRow { HorizontalAlign = HorizontalAlign.Center };
  7. var versionRow = new TableRow { HorizontalAlign = HorizontalAlign.Center };
  8. var cell = new TableCell { HorizontalAlign = HorizontalAlign.Center, Text = string.Format("{0}", name) };
  9. var versionCell = new TableCell { HorizontalAlign = HorizontalAlign.Center, Text = string.Format("{0}", version) };
  10. row.Cells.Add(cell);
  11. versionRow.Cells.Add(versionCell);
  12. tbl.Rows.Add(row);
  13. tbl.Rows.Add(versionRow);
  14. tbl.RenderControl(writer);
  15. }
  16. }

Next, we are going to create a EditorVersionPart superclass that inherits from the EditorPart concrete class. This is the class that will be called from the WebPart superclass, and will leverage the CreateVersionSegement method from above. Because of the class inheritance, we are going to have to implement some abstract inherited methods but are just going to use orthodox filler since we don’t need to implement any sort of behavior for them.

  1. public class EditorVersionPart : EditorPart
  2. {
  3. public string _version;
  4. public string _name;
  5.  
  6. public EditorVersionPart(string webPartID, string name, string version)
  7. {
  8. Title = "WebPart Versioning";
  9. ID = string.Format("MyEditorPart{0}", webPartID);
  10. _name = name;
  11. _version = version;
  12. }
  13.  
  14. protected override void RenderContents(HtmlTextWriter writer)
  15. {
  16. base.RenderContents(writer);
  17. VersionInfo.CreateVersionSegement(writer, _version, _name);
  18. }
  19.  
  20. public override bool ApplyChanges()
  21. {
  22. EnsureChildControls();
  23. return true;
  24. }
  25.  
  26. public override void SyncChanges()
  27. {
  28. EnsureChildControls();
  29. }
  30. }

Moving right along, the WebPartSuperClass while inheriting from the WebPart base class also will inherit from the IWebEditable inertface to specify custom editing controls associated with the WebPart control. In the IWebEditable.CreateEditorParts() method we are going to call our custom EditorPart, then supply the requisite string values.

  1. public class WebPartSuperClass : WebPart, IWebEditable
  2. {
  3.  
  4. public WebPartSuperClass()
  5. {
  6. ExportMode = WebPartExportMode.All;
  7. }
  8.  
  9. EditorPartCollection IWebEditable.CreateEditorParts()
  10. {
  11. var editors = new List<EditorPart> { new EditorVersionPart(ID, "My WebPart", "1.0.0.0") };
  12. return new EditorPartCollection(editors);
  13. }
  14.  
  15. object IWebEditable.WebBrowsableObject
  16. {
  17. get { return this; }
  18. }
  19. }

That’s it!

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>