Adding validation to custom web part properties (the proper way!) Thanks Ken Josling for this info!
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.Xml.Serialization;
using System.IO;
using System.Resources;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace YourNamespace
{
public class YourWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
private bool invalidConfiguration;
private string exceptionMessage;
private ResourceManager resourceManager;
public YourWebPart()
{
invalidConfiguration = false;
exceptionMessage = string.Empty;
resourceManager =
new ResourceManager("YourNamespace.Resource", GetType().Assembly);
}
private void invalidateConfiguration(string message)
{
invalidConfiguration = true;
exceptionMessage = message;
}
protected void AddInitializeControl()
{
bool hasRights = ((ISecurableObject)SPContext.Current.Web).DoesUserHavePermissions((SPBasePermissions)Microsoft.SharePoint.SPPermissionGroup.WebDesigner);
if (!hasRights)
return;
string editLink = Microsoft.SharePoint.WebPartPages.ToolPane.GetShowExtensibleToolPaneEvent(string.Format(@"'{0}'", this.UniqueID));
this.Controls.Add(new LiteralControl(
string.Format(exceptionMessage + resourceManager.GetString("WebPartHelpMessage"), editLink.Replace(@"ExtensibleView", @"Edit"))
));
}
protected override void CreateChildControls()
{
//Add your validation logic here
if(x)
{
invalidateConfiguration(resourceManager.GetString("SomeErrorMessage"));
}
if (y)
{
invalidateConfiguration(resourceManager.GetString("SomeOtherErrorMessage"));
}
if (invalidConfiguration)
{
AddInitializeControl();
}
else
{
//Render Web Part Here
}
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment