SharePoint Developer Center >
SharePoint Products and Technologies Forums
>
SharePoint - Development and Programming
>
validating web part control fields
validating web part control fields
- Hi all,
I'm working on a small web part, that is basically a form. It has some text boxes, drop downs etc.. and a submit button. The fields get submitted to a list, which I have working.
I'd like to do some validation on my fields, so that some are required. Whats the best way to do this programatically, within my web parts code? Also, could anyone provide examples. It doesn't need to be complicated, just if say the name field is empty, I don't want the submission to happen, and then display an error.
Thanks
- newbie.
Answers
- In your CreateChilcControls method of your web part you can use follwing format of code:
txtbox = new TextBod();
txtbox.ID = "txtTest";
vldProjectError = new RequiredFieldValidator();
vldProjectError.ControlToValidate = txtbox.ID;
vldProjectError.ForeColor = Color.Red;
vldProjectError.ErrorMessage = "Please fill up the field";
this.Controls.Add(vldProjectError);- Proposed As Answer byHereICome SharePoint Wednesday, November 11, 2009 12:47 AM
- Marked As Answer byAaron Han - MSFTModeratorFriday, November 13, 2009 10:27 AM
All Replies
- You need to add RequiredFieldValidator for you control what you want to validate.
Fill properties ControlToValidate is Control`s id which must be filled & ErrorMessage is text to show if required field is empty You need to add RequiredFieldValidator for you control what you want to validate.
Fill properties ControlToValidate is Control`s id which must be filled & ErrorMessage is text to show if required field is empty
Edit:
Nvm, I see the RequiredFieldValidator now. Thanks I"ll try this out.- In your CreateChilcControls method of your web part you can use follwing format of code:
txtbox = new TextBod();
txtbox.ID = "txtTest";
vldProjectError = new RequiredFieldValidator();
vldProjectError.ControlToValidate = txtbox.ID;
vldProjectError.ForeColor = Color.Red;
vldProjectError.ErrorMessage = "Please fill up the field";
this.Controls.Add(vldProjectError);- Proposed As Answer byHereICome SharePoint Wednesday, November 11, 2009 12:47 AM
- Marked As Answer byAaron Han - MSFTModeratorFriday, November 13, 2009 10:27 AM
- HereICome, you have forgot to add TextBox to Controls collection ;)
- Jevgeni, That done not matter here in the context of the question. :)


