Ask a questionAsk a question
 

Answervalidating web part control fields

  • Wednesday, November 04, 2009 9:02 PMChrisC_26 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Wednesday, November 04, 2009 10:06 PMHereICome SharePoint Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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);

All Replies

  • Wednesday, November 04, 2009 9:07 PMJevgeni Borozna Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Wednesday, November 04, 2009 9:27 PMChrisC_26 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Wednesday, November 04, 2009 10:06 PMHereICome SharePoint Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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);
  • Thursday, November 05, 2009 7:41 AMJevgeni Borozna Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    HereICome, you have forgot to add TextBox to Controls collection ;)
  • Wednesday, November 11, 2009 12:47 AMHereICome SharePoint Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Jevgeni, That done not matter here in the context of the question. :)