The TextBox Changed event occurs on server side. So it needs a postback. When you set the
AutoPostBack Property of the TextBox to true, a postback will occor when the TextBox control loses focus. To perform the validation, you need to set the
CausesValidation Property of the TextBox to True, because this is False by default (for a Button control, the default value is True!)
Thx for this great answer. I modified my code and got this
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="CustomValid_Master.aspx.vb" Inherits="JK.CustomValid_Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<ajaxtoolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxtoolkit:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!--Invalid if < 100-->
Compare:
<asp:TextBox id="txtCompare"
runat="server" AutoPostBack="True" CausesValidation="True" />
<asp:CompareValidator ID="CompareValidator1"
ControlToValidate="txtCompare"
Display="Dynamic"
Operator="GreaterThanEqual"
ValueToCompare="100"
Text="*"
ErrorMessage="Comparison not valid (Value < 100)!"
runat="server" EnableClientScript="False" ></asp:CompareValidator>
<br />
<!--Invalid if not odd value-->
Custom:
<asp:TextBox id="txtCustom"
runat="server" AutoPostBack="True" CausesValidation="True" />
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="txtCustom"
Display="Dynamic"
ErrorMessage="Custoam not valid (Not an even number)!"
runat="server"
Text="*" EnableClientScript="False" />
<br />
<!--Validation Summary displays only CompareValidator ErrorMessage (CompareValidator, CustomValidator) if both are invalid-->
<asp:ValidationSummary ID="ValidationSummary" runat="server"
CssClass="ValidationSummary" DisplayMode="List"
HeaderText="Bitte überprüfen Sie folgende Eingabe/-n:" />
<br />
<!--Submit Button-->
<asp:Button id="Button1"
Text="Validate"
runat="server"/>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Since I only use server side validation now it works pretty fine, except the following constellation (capitalized letters stand for int values):
I) I type an invalid value 'A' into txtCompare --> ValidationSummary displays Error on txtCompare
II) I type an invalid value 'B' into txtCustom --> ValidationSummary displays Error on txtCompare, txtCustom
III) I type a valid value 'X' into txtCompare --> ValidationSummary displays Error on txtCustom (txtCompare is valid now)
IV) I type a valid value 'Y' into txtCustom --> ValidationSummary displays no error (txtCustom is valid now)
V) I type an invalid value 'C' into txtCompare --> ValidationSummary displays no error (actually txtCompare is invalid now!!)
VI) I type an invalid value 'A' (same as in I) into txtCompare --> ValidationSummary displays Error on txtCompare
When I now type any invalid value beside 'A' into txtCompare it will stay valid! Why? Is it a bug or is it my mistake?
In the example above I used the following values:
A = 1 (invalid on txtCompare)
B = 1 (invalid on txtCustom)
C = 2 (invalid on txtCompare but no error was displayed)
X = 100 (valid on txtCompare)
Y = 12 (valid on txtCustom)
Thx for any help, hope my information helps to solve the problem
ronin47
Member
62 Points
73 Posts
Re: CompareValidator and CustomValidator in one Form
Apr 29, 2012 01:38 PM|LINK
Thx for this great answer. I modified my code and got this
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="CustomValid_Master.aspx.vb" Inherits="JK.CustomValid_Master" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <ajaxtoolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </ajaxtoolkit:ToolkitScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <!--Invalid if < 100--> Compare: <asp:TextBox id="txtCompare" runat="server" AutoPostBack="True" CausesValidation="True" /> <asp:CompareValidator ID="CompareValidator1" ControlToValidate="txtCompare" Display="Dynamic" Operator="GreaterThanEqual" ValueToCompare="100" Text="*" ErrorMessage="Comparison not valid (Value < 100)!" runat="server" EnableClientScript="False" ></asp:CompareValidator> <br /> <!--Invalid if not odd value--> Custom: <asp:TextBox id="txtCustom" runat="server" AutoPostBack="True" CausesValidation="True" /> <asp:CustomValidator id="CustomValidator1" ControlToValidate="txtCustom" Display="Dynamic" ErrorMessage="Custoam not valid (Not an even number)!" runat="server" Text="*" EnableClientScript="False" /> <br /> <!--Validation Summary displays only CompareValidator ErrorMessage (CompareValidator, CustomValidator) if both are invalid--> <asp:ValidationSummary ID="ValidationSummary" runat="server" CssClass="ValidationSummary" DisplayMode="List" HeaderText="Bitte überprüfen Sie folgende Eingabe/-n:" /> <br /> <!--Submit Button--> <asp:Button id="Button1" Text="Validate" runat="server"/> </ContentTemplate> </asp:UpdatePanel> </asp:Content>Since I only use server side validation now it works pretty fine, except the following constellation (capitalized letters stand for int values):
I) I type an invalid value 'A' into txtCompare --> ValidationSummary displays Error on txtCompare
II) I type an invalid value 'B' into txtCustom --> ValidationSummary displays Error on txtCompare, txtCustom
III) I type a valid value 'X' into txtCompare --> ValidationSummary displays Error on txtCustom (txtCompare is valid now)
IV) I type a valid value 'Y' into txtCustom --> ValidationSummary displays no error (txtCustom is valid now)
V) I type an invalid value 'C' into txtCompare --> ValidationSummary displays no error (actually txtCompare is invalid now!!)
VI) I type an invalid value 'A' (same as in I) into txtCompare --> ValidationSummary displays Error on txtCompare
When I now type any invalid value beside 'A' into txtCompare it will stay valid! Why? Is it a bug or is it my mistake?
In the example above I used the following values:
A = 1 (invalid on txtCompare)
B = 1 (invalid on txtCustom)
C = 2 (invalid on txtCompare but no error was displayed)
X = 100 (valid on txtCompare)
Y = 12 (valid on txtCustom)
Thx for any help, hope my information helps to solve the problem