Search code examples
asp.nettriggersdrop-down-menupostbackrequiredfieldvalidator

Required Field Validator gets run on post back trigger


I have a dropdown list on my aspx page on which I have a RequiredFieldValidator applied over it. DropDown code is:

<asp:DropDownList ID="ddlglCategoryId" runat="server" CssClass="textEntry2" 
                                            AutoPostBack="true" ValidationGroup="Save" DataSourceID="dtsglCategoryId" DataTextField="LookupItem"
                                            DataValueField="Id" AppendDataBoundItems="true">
                                            <asp:ListItem Text="All" Selected="True" Value="0"></asp:ListItem>
                                        </asp:DropDownList>

RequiredFieldValidator code is:

<asp:RequiredFieldValidator ID="rfvddlglCategoryId" InitialValue="0" runat="server"
                                            ErrorMessage="Please select category" CssClass="Validations" ControlToValidate="ddlglCategoryId"
                                            ValidationGroup="Save" Display="Dynamic" SetFocusOnError="true">
                                        </asp:RequiredFieldValidator>

I have a post back trigger as well on my aspx page and when I change the category from the dropdown, page posts back and a grid on my page gets updated. But due to post back, the validator message appears and then disappears. I want this to appear only when "All" is selected from dropdown and user clicks save button.

Any guidelines?

Trigger:

</ContentTemplate>
    <Triggers>
        <%--<asp:AsyncPostBackTrigger ControlID="lbFileName" />--%>
        <asp:PostBackTrigger ControlID="btnFileUploadSave" />
    </Triggers>
</asp:UpdatePanel>

Button:

<asp:ImageButton ID="btnFileUploadSave" runat="server" ValidationGroup="Save"
                                                                        ImageUrl="~/App_Themes/Default/images/update.png" ToolTip="Save"  
                                                                        Height="18px" onclick="btnFileUploadSave_Click"/>

Solution

  • You may need to set the AutoPostBack property of the dropdownlist to false. It sounds like a postback is getting fired with the OnSelectedIndexChanged event handler.