473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Custom validator in <ASP:Table>

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

Oct 25 '06 #1
3 8223
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

Oct 27 '06 #2

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

Oct 27 '06 #3
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

Oct 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Drew Pawlik | last post by:
Hey guys, I'm having a problem and need some help. I have an <asp:table ...> in my web page, and this table has 2 rows, the first being a header row, and the second row containing an <asp:button>...
1
by: Ed. | last post by:
I apologize if this is a stupid question. When working with the <asp:Table> component in design mode, how do you select controls placed in the table cells? I can only seem to click on the table...
0
by: Bryan Donaldson | last post by:
I have a table on my web form, declared like this: <asp:table id="tblOverrides" runat="server" enableviewstate=true" cssclass="clsTable"> <asp:tablerow cssclass="clsTblHeader"> <asp:tablecell ...
5
by: Ken Dopierala Jr. | last post by:
Hi, This is just a query about what people use most. Up until today I've been using <asp:Table> tags to build my tables. We just outsourced our HTML design to a local guy and when I got it back...
1
by: Dave Bennett | last post by:
I am hoping someone can help me because I am really beginning to hate IE. I am using the following code to generate a dynamic table. private void buildTable(ArrayList Products) { //Variables...
1
by: Rob Meade | last post by:
Hi all, I recently read a book regarding improving the performance of web pages loading, one of the suggestions was to add <colgroup> and the relevant <col> tags with information such as cell...
2
by: Roy | last post by:
Hey all, Quick question here. Within VS.NET there are various tags such as <asp:Table>, <asp:TableCell>, etc... which can be used in lieu of <Table>, <td>, etc... Why do such replacement tags...
1
by: Dica | last post by:
i've got a couple of controls within an asp:table and a couple outside. when i add a requiredField validator, it looks like i'm only able to see the controls outside the asp:table within the...
1
by: ckb | last post by:
Hi, This bit of code throws error Expected End of statement at line1 <asp:Table Width="100%" > <asp:TableRow id="T2r1"> <asp:TableCell id="T2r1c1" Width="85%"> <asp:PlaceHolder ID="PlaceHolder1"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.