Link to home
Start Free TrialLog in
Avatar of trevoray
trevoray

asked on

how can i create requiredFieldValidator via code-behind?

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)
ASKER CERTIFIED SOLUTION
Avatar of tockhoi
tockhoi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zlatin Zlatev
@trevoray, what actually is wrong with your code?
What should happen and what happens? What you do to reproduce this behaviour?