Adding Validation to Web Parts

Posted by (JavaScript must be enabled to view this email address) on Wed 21 Jan 2009

Chances are if you have developed a web part in the past, it needed some user input. And if it needed user input then it needs validation.

When you develop webparts you are unable to do use the designer (thanks Microsoft) so you have to be a real coder and declare, instantiate and add the controls entirely programmatically. Including your validation controls. Ok, you’ve done that. Why doesn’t your validation work?

First, you MUST (and I cant stress that enough) instantiate the validators in your overridden OnInit method, not in the CreateChildControls() method. Secondly, set up a validation group so that you can control what the validators are validating (you dont want to validate the whole page) and lastly, you need to ensure that the EnableClientScript property is enabled.

        regexpval.ControlToValidate = this.txtEmail.ID;
        regexpval.ValidationExpression = @”\w+([-+.’]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*”;
        regexpval.ErrorMessage = “Please Enter a valid email address.”;
        regexpval.Display = ValidatorDisplay.Dynamic;
        regexpval.EnableClientScript = true;
        regexpval.ValidationGroup = validationGroup;

 

If you’ve been diligent then your code will compile, deploy and even let you place it on the page, but for some weird reason it still doesn’t work as you think it should. AAAAARGH!

Its a small and very simple fix, the text box you are validating might be instantiated but it doesn’t have an ID, D’oh!

All it takes is adding a line like this BEFORE you set up the validators:

 

txtEmail.ID = “txtEmail”;

Your Comments

  1. Posted by Mauro Masucci on 10/21 at 02:21 PM

  2. Posted by Mauro Masucci on 11/04 at 08:02 AM

  3. content; it¡¯s standards compliant; And as it¡¯s in .Net you have used the customE
    Need evening dresses,or gowns? Formal evening dresses from dresses shop, evening gowns from dress4sale,Laundry and dress4sale
    Wedding dresses
    evening dresses
    evening dresses sale
    Dresses sale
    Wide collection of prom dresses, evening dresses and gowns, cocktail dresses, summer dress, 2010 summer dresses,
    little black dresses, beaded dresses
    Abercrombie & Fitch
    Abercrombie
    Abercrombie & Fitch clothes
    replica watches
    Abercrombie Fitch
    Abercrombie Fitch UKrrorPages features of the framework. Brilliant. So why does

    Posted by Mauro Masucci on 11/06 at 12:14 PM

  4. If you¡¯ve been diligent then your code will compile, deploy and even let you place it on the page, but for some weird reason it still doesn¡¯t work as you think it shouldtiffany
    tiffany jewellery
    Tiffany co
    Tiffany and co
    Tiffany sale
    Tiffany Store
    Herve Leger
    Herve Leger outlet
    Herve Leger uk
    Herve Leger skirt
    HerveLeger

    Posted by Mauro Masucci on 11/21 at 08:39 AM

  5. Posted by Mauro Masucci on 11/29 at 08:56 AM

  6. assistant.On Wednesday, Miller told <h3>burberry replica</h3> told The Associated
    Press that he would <u>burberry belts for men</u> would be “taking some time off” following fake cartier roadster following a church vote Tuesday night on burberry replica handbags on his status
    as senior pastor. He cartier panthere stainless steel He said he will resume his pastorate <h2>cartier replica jewelry</h2> pastorate “eventually.“He says church members gave him cartier love charity bracelet him a vote

    Posted by Mauro Masucci on 12/01 at 09:41 AM

  7. Posted by Mauro Masucci on 12/16 at 02:26 AM

  8. Posted by Mauro Masucci on 12/20 at 09:06 AM

  9. Party Dresses Beach Wedding Dresses White Wedding Dresses mother of the bride formal gowns, apparel, outfits in Austin, Texas. Discount modest mother of the groom dress in petite Informal Wedding Dresses Classic Wedding Dresses Fashion Wedding Dresses Elegant Wedding Dresses Sexy Wedding Dresses ordered 4 dresses to be used as bridesmaid dresses in a wedding. I have just received them and they are more beautiful than I imagined. The dresses were made with great skill and craftma Sweet Wedding Dresses Colored Wedding Dresses Red Wedding Dresses Wedding Dresses With Embellishment What fabric is this dress made Silk Wedding Dresses Corset Back Wedding Dresses Simple Wedding Dresses Bridesmaid Dresses What color is shown on the model? Is it silver, taupe, gray? It’s hard to tell because of the Taffetta material. And will this dress be available for a couple months? Plus Size Bridesmaid Dresses Colored Bridesmaid Dresses Black Bridesmaid Dresses

    Posted by Mauro Masucci on 12/25 at 02:24 AM

Commenting is not available in this section entry.
← Back to Blog Homepage

About our Blog

Brantas Limited specialise in Dynamics CRM, SharePoint and System Integration using the Microsoft Platform. We are all experienced developers in various fields with our own specialities complementing those of our team.

We have been working with SharePoint since 2003, including Installation and Administration, Migration, Development and Support.

Related Solutions

    No Related Posts Found

Related Case Studies

    No Related Posts Found