Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation still showing although disabled

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..
    }
}
like image 721
user710502 Avatar asked Feb 14 '12 18:02

user710502


1 Answers

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?

like image 175
Kelsey Avatar answered Oct 14 '22 10:10

Kelsey