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”;
this.Controls.Add(_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”;
this.Controls.Add(_rfvFirstName);

[tweetmeme only_single=”false”]
Advertisements

Got something to add?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s