I have one final bug to resolve in order to complete a tertiary assignment.
I have two CustomValidators. One is working perfectly as expected; it displays the error message, and does not allow updating of its' GridView row if incorrect data is entered.
The second validator, modelled almost exactly the same, displays the appropriate error message properly ... but then allows the Update function!!
I have tried every combination of CausesValidation & ValidationGroup and yet none of them seem to work.
Please help!!!
<asp:TextBox ID="tbxEditAppointDate" runat="server" Text='<%# Bind("Date") %>'
TextMode="Date" OnTextChanged="TbxEditAppointDate_TextChanged" AutoPostBack="True"
Width="135px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEditAppointDate"
runat="server" ControlToValidate="tbxEditAppointDate" ErrorMessage="Appointment Date is a required field"
Font-Bold="True" ForeColor="Red" Text="*" ValidationGroup="EditAppoint"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="custEditAppointDate" runat="server" ControlToValidate="tbxEditAppointDate" OnServerValidate="EditAppointDate_ServerValidate"
Font-Bold="True" ForeColor="Red" Text="*" ValidationGroup="EditAppoint"></asp:CustomValidator>
<asp:ValidationSummary ID="vsEditAppoint" ValidationGroup="EditAppoint"
ForeColor="Red" Font-Bold="True" runat="server" />
<asp:TextBox ID="tbxEditAppointFee" runat="server"
Text='<%# Bind("Fee", "{0:F2}") %>' AutoPostBack="True" Width="85px"
CausesValidation="True" ValidationGroup="EditFee"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEditAppointFee" runat="server" ControlToValidate="tbxEditAppointFee" ErrorMessage="Appoint Fee is a required field" ForeColor="Red" Font-Bold="True" Text="*" ValidationGroup="EditFee"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="custEditAppointFee"
runat="server" ControlToValidate="tbxEditAppointFee" OnServerValidate="EditAppointFee_ServerValidate" Font-Bold="True" ForeColor="Red" Text="*" ValidationGroup="EditFee">
</asp:CustomValidator>
<asp:ValidationSummary ID="vsEditFee" ValidationGroup="EditFee"
ForeColor="Red" Font-Bold="True" runat="server" />
THE ABOVE TEXTBOX, VALIDATORS (2), SUMMARY GROUP AND SERVERVALIDATE() METHOD ALL WORK AS EXPECTED.
SECOND ONE HOWEVER DOES NOT WORK, BUT DISPLAYS THE ERROR MESSAGES AND ALLOWS UPDATING, DESPITE THE IsValid FLAGS BEING CORRECT (I CHECKED).
protected void EditAppointDate_ServerValidate(object source, ServerValidateEventArgs args)
{
CustomValidator dateCheck = ((CustomValidator)source);
TextBox dateLabel = (TextBox)dateCheck.NamingContainer.FindControl("tbxEditAppointDate");
if (dateLabel.ReadOnly == false)
{
// CHECK IF DATE IS VALID (NOT EARLIER THAN TODAY) ie: DateTime.Today.ToShortDateString()
if (Convert.ToDateTime(dateLabel.Text) < Convert.ToDateTime(DateTime.Today.ToShortDateString()))
{
dateCheck.ErrorMessage = "Appointment Date cannot be earlier than Today";
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
}
protected void EditAppointFee_ServerValidate(object source, ServerValidateEventArgs args)
{
CustomValidator feeCheck = ((CustomValidator)source);
TextBox feeLabel = (TextBox)feeCheck.NamingContainer.FindControl("tbxEditAppointFee");
String tempFeeString;
Double tempFee;
if (feeLabel.ReadOnly == false)
{
// CHECK IF FEE IS NUMERIC (STRIP DOLLAR SIGN)
if (feeLabel.Text.Substring(0, 1).Equals("$"))
{
tempFeeString = feeLabel.Text.Substring(1);
}
else
{
tempFeeString = feeLabel.Text.Substring(0);
}
if (!Double.TryParse(tempFeeString, out tempFee))
{
feeCheck.ErrorMessage = "Appoint Fee must be numeric (or monetary)";
args.IsValid = false;
}
// CHECK THAT FEE IS GREATER THAN ZERO AMOUNT
else if (feeLabel.Text == "0.00" || feeLabel.Text == "$0.00")
{
feeCheck.ErrorMessage = "Fee cannot be set to zero (Min: 0.01)";
args.IsValid = false;
}
// CHECK THAT AMOUNT IS NOT NEGATIVE
else if (tempFee < 0.00)
{
feeCheck.ErrorMessage = "Fee cannot be a negative amount (Min: 0.01)";
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
}