Div is not hidden after calling Page_ClientValidate

The following code does not hide the "info" DIV and show the "questions" DIV when Page_ClientValidate is called sets isValid to true. If I set isValid to true or false, it works as expected. Why does Page_ClientValidate prevent the hide/show behaviour to occur?

function hide() {
        var isValid = Page_ClientValidate('validInfo');
        if (isValid) {
            if (!$('[id$=chkConfirm]').is(':checked'))
            { alert("Please confirm and agree with conditions"); }
            else {
                $('#info').hide();
                $('#questions').show();
            }
        }
    }    

Here is the HTML - I shortened it to the main parts and there are only some more txtBoxes and radioLists in the DIVS:

 <div id="info">
    <asp:TextBox ID="tBoxTradingName" runat="server" Width="200"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="tBoxTradingName"
        ValidationGroup="validInfo">Required</asp:RequiredFieldValidator>
    <asp:CheckBox ID="chkConfirm" Text=" I confirm that the information provided above is accurate."
        runat="server" />
    <asp:Button ID="btnNext" Text="Next >>" runat="server" OnClientClick="hide()" Width="100"
        CausesValidation="true" ValidationGroup="validInfo" />
</div>
<div id="questions" style="display:none;">
    <div class="label">
        Do you trade as a
    </div>
    <asp:RadioButtonList ID="rBtnTradeAs" runat="server">
        <asp:ListItem>Sole Proprietor</asp:ListItem>
        <asp:ListItem>Company</asp:ListItem>
        <asp:ListItem>Closed Corporation</asp:ListItem>
        <asp:ListItem>Trust</asp:ListItem>
        <asp:ListItem>Other (please specify)</asp:ListItem>
    </asp:RadioButtonList>
    <asp:TextBox ID="txtBoxTradeAs" runat="server" Width="200"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator30" runat="server" ControlToValidate="rBtnTradeAs"
        ErrorMessage="Required">
    </asp:RequiredFieldValidator>
</div>

Answers


You're calling a clientside function on a Button's OnCLientClick. Then you want to change visibility of some controls via javascript if Page_ClientValidate returns true.

The problem is: the button will cause a postback if no Validator bites. The postback will cause the page to be recreated on serverside so that every clientside change is lost.

So either

  • don't postback (returning false from OnCLientClick will prevent a postback) or
  • change visibility of the controls on serverside(by adding runat="server" to your divs)

Need Your Help

Sed to loop through file and replace placeholder variables

linux bash shell sed debian

I was hoping to use bash to loop through a file and turn all placeholder varialbes into real variables: