• Webpart Development part 5: validating Custom Web Part Properties

    by  • April 6, 2010 • MOSS 2007 • 5 Comments

    In this article we will see how to validate the values entered by the user in custom property of web part.

    In last blog post we added a text box in tool part pane of web part.  In the below code we are trying to validate whether the text, enter by the user, is more than 10 characters.

    public string text;
    [Category("Advanced Settings"),
    Personalizable(PersonalizationScope.Shared),
    WebBrowsable, WebDisplayName("Text"),
    WebDescription("Enter your text")]
    public string Text
    {
    get { return text; }
    set {
    // Validate input
    if(value.Length < 10)
    throw new WebPartPageUserException("Enter minimum 10 charectors in text field.");
    text = value;
    }
    } 

    As you can see in the above code, you can write your own conditions in the set method of the property of the control and throw WebPartPageUserException if error an occurs or if it is an invalid entry.

    In coming articles we will see how to deploy custom web part in master page

    About

    Hojo Clement was working in US Technology in Trivandrum as a Senior Software Engineer. He has 6 years experience in working with Web Development Technologies inlcuding MOSS and ASP.net.

    5 Responses to Webpart Development part 5: validating Custom Web Part Properties

    1. Pingback: Webpart Development part 4: Creating Custom Web Part Properties

    2. Brijesh Shah
      June 9, 2011 at 5:55 am

      Hi Friends,

      I have tried to use your wonderful code to validate my custom control and make that working. but after adding that validation, I lost my running control.

      I am sure there is something very small mistake but i am not get that what is …
      If you have any idea from my below exception message and code snippet please let me know.

      Exception Message:
      ————————————————————————————————-
      Error

      Web Part Error: An error occured while setting the value of this property: SPCustomization.GridTest.RegisterAllListData:ListName – Exception has been thrown by the target of an invocation.

      Show Error Details
      Hide Error Details

      [WebPartPageUserException: An error occured while setting the value of this property: SPCustomization.GridTest.RegisterAllListData:ListName - Exception has been thrown by the target of an invocation.]
      at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.ApplyPropertyState(SerializedWebPart serializedWebPart)
      at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Deserialize(SPWebPartManager webPartManager, XmlNamespaceManager xmlnsManager, Byte[] userData, Byte[] sharedData, String[] links, Type type, SPWeb spWeb)
      at Microsoft.SharePoint.WebPartPages.SPWebPartManager.CreateWebPartsFromRowSetData(Boolean onlyInitializeClosedWebParts)

      Code Snippet:
      —————————————————————————————————
      using System.Web.UI.WebControls.WebParts;
      using System.Web.UI;
      using Microsoft.SharePoint.WebPartPages;

      namespace SPCustomization.GridTest
      {
      public class RegisterAllListData : System.Web.UI.WebControls.WebParts.WebPart
      {
      protected string userControlPath = @”~/usercontrols/”;
      protected string userControlFileName = @”AllListData.ascx”;
      AllListData _control;

      private string _listName;
      [WebBrowsable(true), WebDisplayName("List Name"), WebDescription("Enter ~ Separated List Name to Show Data Here.."), Personalizable(PersonalizationScope.Shared), System.ComponentModel.Category("Settings"), System.ComponentModel.DefaultValue("")]
      public string ListName
      {
      get { return _listName; }
      set
      {
      if (string.IsNullOrEmpty(_listName))
      throw new WebPartPageUserException(“Please enter atleast one List Name here…”);
      else
      _listName = value;
      }
      }
      }

    3. smriti singh
      October 11, 2011 at 11:46 am

      Hi Your article is very helpful. I was able to validate my custom property using it and displayed an error message using “throw new WebPartPageUserException”.
      But I have one more requirement that error message should be displayed to user as well as the property should be set to some default value.

      The custom property accepts some number within a range. If the entered value exceeds that range then error message should be shown and the value should be reset to say, 10.

      I am either able to reset the value or throw exception. But I want to do both.
      Please help

    4. Shoban
      October 13, 2011 at 8:30 am

      Smriti,

      Set Default value first and then show error message and it should work.

      Shoban

    5. Anil Kuchi
      November 11, 2011 at 9:33 pm

      Hi
      Your article is very helpful ,I am using the same thing but for my text box i want the property as a password field ,I know that we have to use custom tool part but will there be any easy way to achieve this ,can you please guide me on this …

    Leave a Reply

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