How to add your own WebPart properties

If you are creating your own part you might want to add a property for the users to set when editing the webpart.

At the top of your web part class add the following code (before any methods).

[FriendlyName("Project Site Template")]
public string ProjectsiteTemplateName { get; set; }

This will add a property (in my case called Project Site Template) for the users to set when editing the web part.

This allows the user configure the web part for what you need (instead of hard coding some things).

[tweetmeme only_single=”false”]

How to add a required field validator to a SharePoint web part.

You just have to set the ID property of the control, then set the control to validate to the ID of the control.

Below is an example

//Declare Controls
private TextBox _txtFirstName = new TextBox();
RequiredFieldValidator _rfvFirstName = new RequiredFieldValidator();

//Add text box
this._txtFirstName.ID = “txtFirstName”;

//Required field validator
_rfvFirstName.Text = “* First Name is required!”;
_rfvFirstName.Display = ValidatorDisplay.Dynamic;
_rfvFirstName.ControlToValidate = _txtFirstName.ID;
_rfvFirstName.ForeColor = Color.Red;
_rfvFirstName.ValidationGroup = “adduser”;

[tweetmeme only_single=”false”]