Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Custom validator in <ASP:Table>

Reply
Thread Tools

Custom validator in <ASP:Table>

 
 
Andy
Guest
Posts: n/a
 
      10-25-2006
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

 
Reply With Quote
 
 
 
 
Andy
Guest
Posts: n/a
 
      10-27-2006
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

 
Reply With Quote
 
 
 
 
Andy
Guest
Posts: n/a
 
      10-27-2006

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

 
Reply With Quote
 
Andy
Guest
Posts: n/a
 
      10-30-2006
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.ServerValidateEventHandl er(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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts Validator - creditCard validator smrimell@gmail.com Java 4 02-13-2007 08:32 PM
How to get custom control to work with custom validator Scott Yenor ASP .Net Web Controls 0 01-27-2004 12:37 AM
Custom Validator Issue Arvind P Rangan ASP .Net 1 11-12-2003 03:28 PM
custom validator Mike P ASP .Net 2 11-05-2003 03:25 PM
Custom Validator Serdar Kalaycę ASP .Net 1 07-08-2003 01:36 PM



Advertisments