Custom validator in <ASP:Table>

A

Andy

Hi folks,

I have a customvalidator control that works properly if it isn't
contained in an ASP:TABLE. But, when I place it inside an ASP:TABLE, I
find that _ServerValidate doesn't get fired, even if I force the
customvalidator to validate by invoking its validate() method after a
button is clicked:

cvCustomValidator_ServerValidate(object source,
system.Web.UI.WebControls.ServerValidateEventArgs args) <== doesn't get
called

<asp:table id="tblTable" runat="server" Width="100%">
<asp:TableRow>
<asp:TableCell>
<asp:TextBox runat="server" Width="400px" Font-Names="Verdana"
ID="txt_textbox"></asp:TextBox>
<asp:CustomValidator runat="server" ControlToValidate="txt_textbox"
ErrorMessage="This is an error"
ID="cvCustomValidator" Visible="true" Display="Static"
EnableClientScript="false" Enabled="true"
EnableViewState="true">*</asp:CustomValidator>
</asp:TableCell>
</asp:TableRow>
</asp:table>

I don't have any client-side validation set for the validator, and when
I run a test, I key characters into the textbox. I know the
servervalidate method isn't being called because I write to a log from
inside ServerValidate when its called.

Does anyone know what's going wrong?

Is there some parameter I have to set to enable the validator to work
from inside an ASP:TABLE?

Andy
 
A

Andy

After some reading and testing, it seems that the custom validator
doesn't fire an event to trigger a server-side validation routine after
the form is posted when there is no client-side validation routine
assigned to the custom validator in its ClientValidationFunction
property, regardless if the need for such a function is actually
specified by the validator's enableClientScript property.

When I assign a dummy function to the page's client-side jscript, such
as:

function customvalidator(source,args){
args.IsValid=true;
}

the server-side validation routine gets invoked properly after the form
is posted.

Validators can be contained in ASP:TABLEs, as long as the control being
validated is inside the same tablecell that the validator is in. The
same holds true for any other web control that can contain a validator
(such as a panel) - the field being validated HAS to be in the same
container as the validator.

Andy
 
A

Andy

Hey gang,

If you find your server-side customValidation handler STILL isn't being
called after you've set your client-side validiation routine, try this:

Set the OnServerValidate attribute of your custom validator to your
validating function's name. This can be done on your webpage as:

<asp:customvalidator id="cvEmailAddress" runat="server"
OnServerValidate="emailcheck">*</asp:customvalidator>

or in your codebehind in your Page Load (I've used C#) as:
tb_ApplicantEmail.Attributes.Add("OnServerValidate","emailcheck");


Then, in your codebehind, make sure that your validating function is
externally accessible (I've made mine protected - NEVER use public for
this):

protected void emailcheck(object source,
System.Web.UI.WebControls.ServerValidateEventArgs args)
{
u.sendReport("emailcheck"); //this is my logging routine
bool blnResult=false;

args.IsValid=blnResult; //read via Page.IsValid
}


Andy
 
A

Andy

The custom validator seems to be one of those components in .NET that
exposes several ways to do the same thing, and this may result in
problems if you try to mix them.

If after following the previous posts you find now that your custom
validator is firing too many times (ie twice for each user event), its
probably because you are now calling it twice.

How?

Well, the custom validator CAN fire on its own, however, Visual
Studio's Designer has to attach your validation method to the custom
validator control. It does this by writing out a line in the
InitializeComponent() method of your program:

private void InitializeComponent(){
this.cvEmailAddress.ServerValidate += new
System.Web.UI.WebControls.ServerValidateEventHandler(this.emailcheck);
}

If you cut and paste controls or rename validation handlers in your
source code, the designer may not have written this line out
immeadiately (or the existing line contains a reference to an obsolete
validation method). This results in what looks like an unresponsive
custom validator control (the previous posts remedy this by adding the
event handler to the HTML code of your webpage).

But, there maybe a situation later on in your visual studio editing
sessions that causes the Visual Studio designer to properly write out
(or update) the line that attaches your validation method to the custom
validator event handler. When this happens, you end up with two places
where your validation code is called.

The first is from the OnServerValidate event handler in your HTML page
described earlier in this chain. The second is from the new event
handler linkage the Visual Studio Designer just created. When this
happened to me, I found that I liked the InitializeComponent linkage
better so I dropped the OnServerValidate reference in my HTML page.

Hope this helps somebody. Andy
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top