Anyname Donotcare
Anyname Donotcare

Reputation: 11403

Why my form make postback ,althouth there 's a required field validtion wasn't matched?

Q:

I have a RequiredFieldValidator on my page ,when i click the button which is (postback triggered) it makes the postback, although the field which is required wasn't entered ! What's the reason to this behavior?

My .aspx:

                       <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                            <Triggers>
                                <asp:PostBackTrigger ControlID="btn_Search" />
                            </Triggers>
                            <ContentTemplate>
                                <asp:ImageButton ID="btn_Search" runat="server" ImageUrl="~/Images/save.png" 
                                    OnClick="btn_Search_Click" OnClientClick="return postbackButtonClick();" />
                            </ContentTemplate>
                        </asp:UpdatePanel>

                     <asp:DropDownList ID="ddl_department" runat="server" Width="200px" OnSelectedIndexChanged="ddl_department_SelectedIndexChanged"
                            AutoPostBack="True">
                        </asp:DropDownList>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ddl_department"
                            Display="Dynamic" ErrorMessage="*" InitialValue="-1"></asp:RequiredFieldValidator>

                        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                            <ContentTemplate>
                                <asp:DropDownList ID="ddl_study" runat="server" Width="200px">
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="ddl_study"
                                    Display="Dynamic" ErrorMessage="*" InitialValue="-1"></asp:RequiredFieldValidator>
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="ddl_department" EventName="SelectedIndexChanged" />
                            </Triggers>
                        </asp:UpdatePanel>

                        <script type="text/javascript">
                            var updateProgress = null;
                            function postbackButtonClick() {
                                updateProgress = $find("<%= UpdateProgress1.ClientID %>");
                                window.setTimeout("updateProgress.set_visible(true)", updateProgress.get_displayAfter());
                                return true;
                            }
                        </script>

Upvotes: 1

Views: 535

Answers (1)

IUnknown
IUnknown

Reputation: 22458

Remove return clause from LinkButton's OnClientClick property value: OnClientClick="postbackButtonClick()" and rewrite postbackButtonClick as below:

function postbackButtonClick() {
     Page_ClientValidate();

     if (Page_IsValid) {
          updateProgress = $find("<%= UpdateProgress1.ClientID %>");
          window.setTimeout("updateProgress.set_visible(true)", updateProgress.get_displayAfter());
          return true;
     }
}

Upvotes: 1

Related Questions