RequiredFieldValidator + Button = Postback!

Luigi Saggese picture Luigi Saggese · Apr 18, 2011 · Viewed 7.4k times · Source

i have this markup code:

<div id="rowIndirizzo" class="form">
    <label>
        Indirizzo:</label>
    <asp:DropDownList ID="ddlToponimo" CssClass="indirizzo" runat="server" DataTextField="Toponimo"
        DataValueField="Toponimo">
    </asp:DropDownList>
    <asp:TextBox ID="txtStrada" CssClass="textinputStrada" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtNumCivico" CssClass="textinputCivico" runat="server"></asp:TextBox>
</div>
<div id="divCap" class="form">
    <label>
        CAP:</label>
    <asp:TextBox ID="txtCAP" CssClass="textinputCAP" runat="server"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="rfvToponimo" CssClass="errore2" runat="server" ControlToValidate="ddlToponimo"
    ErrorMessage="Il campo toponimo dell'indirizzo è obbligatorio." Display="Dynamic"
    ValidationGroup="Step4_StruttPrivata1Step" Style="color: Red; display: block !important;"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="rfvStrada" CssClass="errore2" runat="server" ControlToValidate="txtStrada"
    ErrorMessage="Il campo strada dell'indirizzo è obbligatorio." Display="Dynamic"
    ValidationGroup="Step4_StruttPrivata1Step" Style="color: Red; display: block !important;"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="rfvNumCivico" CssClass="errore2" runat="server" ControlToValidate="txtNumCivico"
    ErrorMessage="Il campo numero civico dell'indirizzo è obbligatorio." Display="Dynamic"
    ValidationGroup="Step4_StruttPrivata1Step" Style="color: Red; display: block !important;"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="rfvCAP" CssClass="errore2" runat="server" ControlToValidate="txtCAP"
    ErrorMessage="Il campo CAP è obbligatorio." Display="Dynamic" 
    ValidationGroup="Step4_StruttPrivata1Step" Style="color: Red;
    display: block !important;"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revCAP" runat="server" ControlToValidate="txtCAP"
    ValidationExpression="^([0-9]{5})$" ErrorMessage="Il formato del CAP è errato (Es. 01234)"
    Display="Dynamic" ValidationGroup="Step4_StruttPrivata1Step" Style="color: Red;
    display: block !important;" />
<asp:Button ID="btnSearchIndirizzo" OnClientClick="return false;" class="buttonnext" runat="server" Text="Inserisci"
    ValidationGroup="Step4_StruttPrivata1Step"></asp:Button>

I have some control to validate on button click, but i don't want a postback (i can't use input button because dont fire rfv). I have tried with onclientclick event...but nothing.

Answer

Nikhil picture Nikhil · Apr 18, 2011

Update: I have modified the dummyFunction code to explicitly fire client side validation and to stop postback if the page is invalid.

Try the following attribute on your button markup

UseSubmitBehavior = false

You might also want to try

OnClientClick = "return dummyFunction();"

And then have a JS function dummyFunction such as

 function dummyFunction(){

//this command will explicitly fire the validation
    Page_ClientValidate();
    return Page_IsValid; //Page_IsValid is set to true or false depending on whether a page is valid or not after client validation
    }