how can i create requiredFieldValidator via code-behind?

trevoray
trevoray used Ask the Experts™
on
hi, previous someone gave me the following answer for creating a validator for field behind. it worked, but it wasn't the validator i needed. i tried to reformat it for mine and i can't get it to work. i'll post previous answer first, and then my retooling. can anyone help me how to retool this to make it a RequiredFieldValidator? thanks!

previous answer:
Accepted Answer from detaybey                        
            Date: 08/23/2003 10:44PM PDT          

             I believe dummy field could create problems (cause validator will try to check that dummy)

anyways, there is a better way..

put a dummy LABEL to form (i.e. under textbox) .. and try this;

-------

Dim myCompare As CompareValidator = New CompareValidator

        With myCompare
            .ControlToValidate = "TextBox1"         '
            .ErrorMessage = "Numbers Only"
            .Display = ValidatorDisplay.Dynamic
            .Operator = ValidationCompareOperator.DataTypeCheck
            .Type = ValidationDataType.Integer
            .EnableClientScript = True
        End With

Label1.Controls.Add(myCompare)

------

this is creating a datatypecheck validator via behind-code. you can create any validator when ever you need with this way.

EmRe
           
-------------------------

My "re-tooling" to get it to work for RequiredFieldValidator, which hasn't worked yet.

Dim myRequired1 As RequiredFieldValidator = New RequiredFieldValidator
                With myRequired1
                    .ControlToValidate = "pub1"
                    .ErrorMessage = "Publication #1"
                    .Display = ValidatorDisplay.Dynamic
                    .Text = "Please provide publication."
                End With

         

                PubDummy1.Controls.Add(myRequired1)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Here's what I did and it worked:

- Used your code
- PubDummy is a label
- Left the textbox empty
- Added and pressed a button to cause a postback
- Error message appears

Or am I reading your problem wrong?
Zlatin ZlatevSenior Technical Architect, Salesforce Commerce Cloud

Commented:
@trevoray, what actually is wrong with your code?
What should happen and what happens? What you do to reproduce this behaviour?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial