Custom Validator Control - newbie

A

Angela

Hi

I am having trouble writing a custom control for validation.

I have a really long form with loads of textboxes that I want a custom
control to validate for 'required' and 'range of 0-9'

That's so I can drag and drop my custom control and pass in properties:
controltoValidate and errormessage.

It seems to have problems passing in the ControlToValidate, what I'm I
missing?

Thanks, Angela


Here is the ascx I have started writing:

<script language="VB" runat="server">

Public Property reqControlToValidate As String
Get
Return reqField.controlToValidate
End Get
Set
reqField.controlToValidate = Value
End Set
End Property

Public Property ErrMsg as string
get
return reqField.ErrorMessage

end get
set
reqField.ErrorMessage = value
end set
end property

Public Property ranControlToValidate As String
Get
Return ranVal.controlToValidate
End Get
Set
ranVal.controlToValidate = Value
End Set
End Property

Public Property RanErrMsg as string
get
return ranVal.ErrorMessage

end get
set
ranVal.ErrorMessage = value
end set
end property

</script>

<asp:RequiredFieldValidator ID="reqField"
runat="server">*</asp:RequiredFieldValidator>

<asp:RangeValidator id="ranVal" runat="server" Type="Integer"
MinimumValue="0" MaximumValue="9">*</asp:rangevalidator>

-------------------------------------------------------

And the aspx looks something like:

<%@ Register TagPreFix="Validation" TagName="Test"
src="TestValidation.ascx" %>

<asp:textbox id="txtEI" runat="server"></asp:textbox>

<validation:test runat="server" id="test1" ErrMsg="Message"
reqControlToValidate="txtEI" ranControlToValidate="txtEI" RanErrMsg
="Message2"/>
 
J

John Saunders

Angela said:
Hi

I am having trouble writing a custom control for validation.

I have a really long form with loads of textboxes that I want a custom
control to validate for 'required' and 'range of 0-9'

That's so I can drag and drop my custom control and pass in properties:
controltoValidate and errormessage.

It seems to have problems passing in the ControlToValidate, what I'm I
missing?

Thanks, Angela


Here is the ascx I have started writing:

<script language="VB" runat="server">

Public Property reqControlToValidate As String
Get
Return reqField.controlToValidate
End Get
Set
reqField.controlToValidate = Value
End Set
End Property

Public Property ErrMsg as string
get
return reqField.ErrorMessage

end get
set
reqField.ErrorMessage = value
end set
end property

Public Property ranControlToValidate As String
Get
Return ranVal.controlToValidate
End Get
Set
ranVal.controlToValidate = Value
End Set
End Property

Public Property RanErrMsg as string
get
return ranVal.ErrorMessage

end get
set
ranVal.ErrorMessage = value
end set
end property

</script>

<asp:RequiredFieldValidator ID="reqField"
runat="server">*</asp:RequiredFieldValidator>

<asp:RangeValidator id="ranVal" runat="server" Type="Integer"
MinimumValue="0" MaximumValue="9">*</asp:rangevalidator>

-------------------------------------------------------

And the aspx looks something like:

<%@ Register TagPreFix="Validation" TagName="Test"
src="TestValidation.ascx" %>

<asp:textbox id="txtEI" runat="server"></asp:textbox>

<validation:test runat="server" id="test1" ErrMsg="Message"
reqControlToValidate="txtEI" ranControlToValidate="txtEI" RanErrMsg
="Message2"/>

You didn't say what error you're seeing, so this is just a guess: both the
validation control and the control being validated need to be within the
same naming container. You've got the validation controls inside of one
naming container (the user control), and the text box inside of a different
naming container. That won't work.
 
A

Angela

Thanks for the reply

The error message is

Unable to find control id 'txtEI' referenced by the 'ControlToValidate'
property of 'reqField'.

Does that mean you can't dynamically validate a control with a custom
control?
 
S

Shan Plourde

With the CustomValidator control you don't specify the ControlToValidate
property. You have to instead specify your custom validation method. In
your validation method you can then validate any combination of controls
that you need.
 
S

Shan Plourde

<asp:CustomValidator
OnServerValidate="MyValidateMethod"
Display="Static"
Visible="True"
Enabled="False"
EnableClientScript="False"
ErrorMessage="You Must Specify the Month, Day, and Year for
Effective Date"
runat="server">
<img src='/images/common/WarningIcon.gif'>
</asp:CustomValidator>

protected void MyValidateMethod(object o, ServerValidateEventArgs a)
{
a.IsValid = <Your Validation Expression>
}

If a.IsValid = true then validation passes. If you set to false,
validation fails. That's pretty much it.

MSDN help has a lot of information and examples as well. Your validation
method can do anything that you need it to. You can find tons of
resources for this by doing google searches also...

http://msdn.microsoft.com/library/d...webuiwebcontrolscustomvalidatorclasstopic.asp

Shan Plourde
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top