using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Microsoft.SharePoint.WebPartPages;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Xml.Serialization;
namespace MyWebParts
{
/// <summary>
/// Summary description for WebCustomControl1.
/// </summary>
[ToolboxData("<{0}:CustomPropertyWebPart runat=server></{0}:CustomPropertyWebPart>")]
[GuidAttribute("97657068-289B-4b83-96A4-8BFE989C9FEF")]
[XmlRoot(Namespace="http://thomriz/samples/")]
public class CustomPropertyWebPart : WebPart
{
// Constants
const string strDefault = "This is a Sample String.";
const bool boolDefault = false;
const int intDefault = 20;
const float floatDefault = 33.33f;
public enum enumSample
{
Black=0,
Green,
Blue,
Yellow
};
protected enumSample enumInstance;
// Private variables
private string strMyString;
private string strMyStringforToolPart;
private bool boolMyBoolean;
private int intMyInteger;
private float floatMyFloat;
private System.DateTime datetimeMyDateTime;
private System.Drawing.KnownColor knowncolorMyColor =
System.Drawing.KnownColor.Red;
// Constructor
public CustomPropertyWebPart()
{
// Initialize private variables.
strMyString = strDefault;
strMyStringforToolPart = strDefault;
boolMyBoolean = boolDefault;
intMyInteger = intDefault;
floatMyFloat = floatDefault;
enumInstance = enumSample.Black;
datetimeMyDateTime = System.DateTime.Now;
}
// Creates a custom property that is a String.
// This property will display as a text box in the
// property pane.
// Create a custom category in the property sheet.
[Category("Custom Properties")]
// Assign the default value.
[DefaultValue(strDefault)]
// Property is available in both Personalization
// and Customization mode.
[WebPartStorage(Storage.Personal)]
// The caption that appears in the property sheet.
[FriendlyNameAttribute("Custom String")]
// The tooltip that appears when hovering over the
// friendly name in the property pane.
[Description("Type a string value.")]
// Display the property in the property pane.
[Browsable(true)]
[HtmlDesignerAttribute(BrowserBuilderType.Dynamic)]
// The accessor for this property.
public string MyString
{
get
{
return strMyString;
}
set
{
strMyString = value;
}
}
// Creates a custom property that is a Boolean.
// This property will display as a checkbox in the
// property pane.
[Category("Custom Properties")]
[DefaultValue(boolDefault)]
[WebPartStorage(Storage.Personal)]
[FriendlyNameAttribute("Custom Boolean")]
[Description("Select to set value to True.")]
[Browsable(true)]
// The accessor for this property.
public bool MyBool
{
get
{
return boolMyBoolean;
}
set
{
boolMyBoolean = value;
}
}
[Category("Custom Properties")]
[DefaultValue(strDefault)]
[WebPartStorage(Storage.Personal)]
[FriendlyNameAttribute("Custom String for Toolpart")]
[Description("Used by the toolpart.")]
[Browsable(false)]
// The accessor for this property.
public string MyStringforToolPart
{
get
{
return strMyStringforToolPart;
}
set
{
strMyStringforToolPart = value;
}
}
// Creates a custom property that is an integer.
// This property will display as a text box in the
// property pane.
[Category("Custom Properties")]
[DefaultValue(intDefault)]
[WebPartStorage(Storage.Personal)]
[FriendlyNameAttribute("Custom Integer")]
[Description("Type an integer value.") ]
[Browsable(true)]
public int MyInt
{
get
{
return intMyInteger;
}
set
{
intMyInteger = value;
}
}
// Creates a custom property that is a Float.
// This property will display as a text box in the
// property pane.
[Category("Custom Properties")]
[DefaultValue(floatDefault)]
[WebPartStorage(Storage.Personal)]
[FriendlyNameAttribute("Custom Float")]
[Description("Type a floating point value.") ]
[Browsable(true)]
public float MyFloat
{
get
{
return floatMyFloat;
}
set
{
floatMyFloat = value;
}
}
// Creates a custom property that is a System.DateTime
// This property will display as a text box in the
// property pane.
[Category("Custom Properties")]
[WebPartStorage(Storage.Personal)]
[FriendlyNameAttribute("Custom Date Time")]
[Description("Type a DateTime value.")]
[Browsable(true)]
public System.DateTime MyDateTime
{
get
{
return datetimeMyDateTime;
}
set
{
datetimeMyDateTime = value;
}
}
public bool ShouldSerializeMyDateTime()
{
return true;
}
// Creates a custom property that is an Enum.
// This property will display as a dropdown list in the
// property pane.
[Category("Custom Properties")]
[DefaultValue(enumSample.Black)]
[WebPartStorage(Storage.Personal)]
[FriendlyName("Custom Enum")]
[Description("Select a value from the dropdown list.")]
[Browsable(true)]
[HtmlDesignerAttribute(BrowserBuilderType.Dynamic,DialogFeatures="center:yes;dialogHeight=11;dialogWidth=12;status=no;resizable=no;unadorned=yes;")]
public enumSample MyEnum
{
get
{
return enumInstance;
}
set
{
enumInstance = value;
}
}
protected override string GetCustomBuilder(string propertyname)
{
if(propertyname=="MyEnum")
return ReplaceTokens("_WPR_/colorbuilder.htm");
if(propertyname=="MyString")
return ReplaceTokens("_WPR_/colorbuilder.htm");
return null;
}
// Creates a property that is a known system color.
// This property will display as a dropdown list in the
// property pane.
[Category("Custom Properties")]
[WebPartStorage(Storage.Personal)]
[FriendlyNameAttribute("Custom Color")]
[Description("Select a color from the dropdown list.")]
[Browsable(true)]
public System.Drawing.KnownColor MyColor
{
get
{
return knowncolorMyColor;
}
set
{
knowncolorMyColor = value;
}
}
public bool ShouldSerializeMyColor()
{
return true;
}
// Render the Web Part
protected override void RenderWebPart(HtmlTextWriter output)
{
// Write stored property values to the Web Part
output.Write("<div id='mydiv_" + this.ReplaceTokens("_WPQ_") + "' STYLE='background-color:" + this.MyEnum.ToString() + "'>");
output.Write("<b>Stored Property Values</b>");
output.Write("<br><b>String: </b>" +
this.MyString);
output.Write("<br><b>Boolean: </b>" +
this.MyBool.ToString());
output.Write("<br><b>Int: </b>" +
this.MyInt.ToString());
output.Write("<br><b>Float: </b>" +
this.MyFloat.ToString());
output.Write("<br><b>DateTime: </b>" +
this.MyDateTime.ToString());
output.Write("<br><b>Hour: </b>" +
this.MyDateTime.Hour.ToString());
output.Write("<br><b>Enum: </b>" +
this.MyEnum.ToString());
output.Write("<br><font color='" + this.MyColor.ToString() + "'>Color Enum: </font>" +
this.MyColor.ToString());
output.Write("</div>");
output.Write("<BR><BR><B>Token replacement</B>");
/*output.Write("<br><b>Current User: </b> " +
this.ReplaceTokens("_LogonUser_"));
*/
output.Write("<br><b>Class Resource Path: </b> " +
this.ReplaceTokens("_WPR_"));
output.Write("<br><b>Qualifier: </b> "
+ this.ReplaceTokens("_WPQ_"));
output.Write("<br><b>Web Part ID: </b> "
+ this.ReplaceTokens("_WPID_"));
output.Write("<br><br><b>String from Tool Part: </b> "
+ this.MyStringforToolPart.ToString());
}
public override ToolPart[] GetToolParts()
{
ToolPart[] toolparts = new ToolPart[3];
toolparts[0] = new WebPartToolPart();
toolparts[1] = new CustomPropertyToolPart();
//Create a custom toolpart
toolparts[2] = new MyWebParts.CustomToolPart();
return toolparts;
}
}
}