Friday, 18 January 2008

MOSS 2007 WebPart

Here is some code for a WebPart that is a clickable image (you'll need to edit the out HTML):

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;


namespace FMWebPartImage
{
[Guid("4f37cd37-d097-41c1-9c1c-37d8faea816d")]
[XmlRoot(Namespace = "FMWebPartImage")]
[ToolboxData("<{0}:WebPart1 runat=server>")]
public class FMWebPartImage : Microsoft.SharePoint.WebPartPages.WebPart
{
public FMWebPartImage()
{
this.ExportMode = WebPartExportMode.NonSensitiveData; //Set so no export
this.ChromeType = PartChromeType.None; //Set so no title by default
}
protected string imageLocation = "";
protected string imageAlternateText = "";
protected string imageURL = "";
protected int imageHeight = 100;
protected int imageWidth = 100;
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(""),
WebPartStorage(Storage.Shared),
FriendlyName("Location"),
Description("Location of image")]
public string ImageLocation
{
get
{
return this.imageLocation;
}
set
{
this.imageLocation = value;
}
}
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(""),
WebPartStorage(Storage.Shared),
FriendlyName("Alternate Text"),
Description("Alternate text for image")]
public string ImageAlternateText
{
get
{
return this.imageAlternateText;
}
set
{
this.imageAlternateText = value;
}
}
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(""),
WebPartStorage(Storage.Shared),
FriendlyName("Image Url"),
Description("Image click goto Url")]
public string ImageClickUrl
{
get
{
return this.imageURL;
}
set
{
this.imageURL = value;
}
}
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(100),
WebPartStorage(Storage.Shared),
FriendlyName("Height"),
Description("Height of image")]
public int ImageHeight
{
get
{
return this.imageHeight;
}
set
{
this.imageHeight = value;
}
}
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(100),
WebPartStorage(Storage.Shared),
FriendlyName("Width"),
Description("Width of image")]
public int ImageWidth
{
get
{
return this.imageWidth;
}
set
{
this.imageWidth = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
string outHTML =
"<a href='" + imageURL + "' style='text-decoration:none;'><img src = '" + imageLocation +
"' alt = '" + imageAlternateText +
"' height = '" + imageHeight +
"' width = '" + imageWidth +
"' /%gt;</a>";
writer.Write(outHTML);
}
}
}

1 comment:

seelan said...

i have created webpart for ur code but how can i set img to img area