5

I have a user control where I have two panels, one is on the left and the other on the right, on the left there is some customer billing information and on the right some shipping information, each field in the shipping information has a requiredfieldvalidator, these panels are wrapped in a update panel. The default state is that both panels are visible, below the panels there is a checkbox that says "Shipping same as billing", when you check it the panel on the right "Shipping Panel" disappears and then you continue.

Issue: Lets say I remove the text in the Shipping Name and I dont click anywhere else, then i go to the checkbox "Same as billing" for a moment it shows the validation in red and then it dissapears. I have tried creating a function that disables the validator i.e

RequireFieldValidator1.Enabled = false;

It works fine in the terms it does not show the validation before the shipping panel dissapears, but lets say I change my mind and click it again to display it. Now when I erase the name in the shipping panel and click somewhere else, it does not let me go anywhere but it doesnt show the validation text.

So I did this logic:

if (ckSameBilling.checked)
{
     RequiredFieldValidator.Enabled = false;
}
else
{
      RequiredFieldValidator.Enabled = true;
}

But now it went back to the same behavior like I demostrated above, clear the Shipping Name and click checbox.. i can see the red validation momentarily.

Related code

//All this is wrapped in an update panel
<asp:Panel ID="pnl" runat="server"><div>
    asp:TextBox ID="txtShippingFirstName" runat="server" Width="130px" Columns="30"
     MaxLength="100" asp:TextBox><div>

<asp:RequiredFieldValidator ID="Requiredfieldvalidator1" ErrorMessage="Name Required"
    ControlToValidate="txt" 
    runat="server" Display="Dynamic" CssClass="Error">
</asp:RequiredFieldValidator>
</asp:Panel>

<div style="margin-left: 145px;">
    <asp:CheckBox ID="Billing" runat="server" Font-Bold="True"
        Text"Same as Billing" OnCheckedChanged="Billing_CheckedChanged"
        AutoPostBack="True" />
</div>

Codebehind:

protected void Billing_CheckedChanged(object sender, EventArgs e)
{    
    if (Billing.Checked)
    {
        //Disable the shipping fields validations
        DisableEnable(true);
        pnl.Visible = false;
    }
    else
    {
        DisableEnable(false);
        pnl.Visible = true;
    }
}

private void DisableEnable(bool enable)
{
    if (enableFields)
    {
        Requiredfieldvalidator1.Enabled = false;
        //a bunch of required validations below...
    }
    else
    {
        Requiredfieldvalidator1.Enabled = true;
        //a bunch of required validations below..
    }
}
user710502
  • 11,181
  • 29
  • 106
  • 161

2 Answers2

5

Have you tried disabling the validators in Javascript (not server side) before you do the postback of the UpdatePanel?

You can use the following function client side to disable your validators:

var yourValidator = document.getElementById('yourValidatorClientID')
ValidatorEnable(yourValidator, false);

This should stop that 'flash' of validation. You could base it of your CheckBox change client side.

You could also include all your validators in seperate groups for each section and then disable an entire group of validators at once. See the following SO thread for more info:

Enable/disable asp.net validator controls within a specific "ValidationGroup" with jQuery?

Community
  • 1
  • 1
Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • using the jquery approach, thank you so much.. I am not a guru at it but how can i check the state of the checkbox in jquery? (whether its checked or not) ? Thank you once again – user710502 Feb 14 '12 at 18:42
0
private void DisableEnableShippingFieldsValidations(bool enableFields)
{
    if (enableFields)
        ckBilling.CausesValidation = false;
    else
        ckBilling.CausesValidation = true;
}

By using the causes validation property of the checkbox, you are by passing the validation.


Suggestion

While doing validations, Please use Validation Group property for the controls to validate and for the button/CheckBox which will do the validation.

user710502
  • 11,181
  • 29
  • 106
  • 161
Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • Why not re-write that method to the one line: `ckSameBilling.CausesValidation = !enableFields;`? In my experience, multiple code paths is far harder to read than a single code path that relies on data. (I'd _laugh_ at that last sentence if someone else wrote it without supporting examples, but here's the perfect supporting example! :) – sarnold Feb 14 '12 at 22:34