Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net postback prevented after clientside validation

I have an asp.net form which contains a dropdownlist which posts back to the server on change and populates second dropdownlist with some dates.

The form also contains other fields some of which are validated clientside and some server side.

Here's the problem I'm having. If I get a clientside validation error then try to change the dropdownlist, the second dropdown does not get populated. If I then change the first dropdownlist again, it works as expected.

Here's my submit button:

<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClientClick="Page_ClientValidate(); return checkPassengers();" OnClick="Page_Transfer" ValidationGroup="FormSubmit" />

Here's my clientside validation:

function checkPassengers() {
    if($("#testField").val() == "Name *" || $("#testField").val() == "") {
            $("#pltester").prepend("<p class='fillall'>Please fill in all fields marked with *</p>");
            return false;       
    }
};

Dropdowns:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddl1st" EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <asp:DropDownList ID="ddl1st" Width="190" AutoPostBack="true" OnSelectedIndexChanged="ChooseDates1st" runat="server" />
        <asp:DropDownList ID="ddlDepart1st" AutoPostBack="true" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>
like image 927
Tom Avatar asked Jan 16 '23 21:01

Tom


1 Answers

I've ran into this problem many times before when using updatepanels.

I've found that if the field needs to be validated then you have to actually set CausesValidation="true" on the element for it to still work with updatepanels.

Hope this helps you out!

like image 95
Jamie Taylor Avatar answered Jan 20 '23 04:01

Jamie Taylor