Tuesday, June 14, 2011

Webpart Development part : validating Custom Web Part Properties!

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

No comments:

Post a Comment