ASP.NET Dropdownlist PostBack make requiredfield validator disappear

Last Reply 10 days ago By pandeyism

Posted 10 days ago

Hi EveryOne

When i click the signup button, required validatation appears. when i choose dropdownlist before entering data in textbox. the required validations disappear.

So how i should prevent disappearing, keep in mind i need the autopostback = true, in order to obtain the value of dropdownlist on button click

please excuse my poor english

    <form id="form1" runat="server">
        <div class="form-group">
            <label class="control-label">
                Name</label>

            <asp:TextBox ID="TextBoxName" runat="server" CssClass="form-control"
                Font-Size="Large"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator8" ControlToValidate="TextBoxName" runat="server" ErrorMessage="*" ValidationGroup="S" ForeColor="Red"></asp:RequiredFieldValidator>
        </div>

        <div class="form-group">
            <label class="control-label">
                Gender</label>
            <asp:DropDownList ID="DDLforGender" runat="server" class="form-title form-control"
                Font-Size="Large" AutoPostBack="True" ForeColor="#888888">
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="DDLforGender" InitialValue="0" runat="server" ValidationGroup="S" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
        </div>
        
   <asp:Button ID="Button6" runat="server" CssClass="btn btn-success large-text btn-lg" OnClick="Button6_Click" Text="SignUp" UseSubmitBehavior="false" ValidationGroup="S" />

    </form>

 

You are viewing reply posted by: pandeyism 10 days ago.
Posted 10 days ago

Hi ahmedsamy,

You need to make selected true refer below code.

HTML

<div class="form-group">
    <label class="control-label">
        Name</label>
    <asp:TextBox ID="TextBoxName" runat="server" CssClass="form-control" Font-Size="Large"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator8" ControlToValidate="TextBoxName"
        runat="server" ErrorMessage="*" ValidationGroup="S" ForeColor="Red"></asp:RequiredFieldValidator>
</div>
<div class="form-group">
    <label class="control-label">
        Gender</label>
    <asp:DropDownList ID="DDLforGender" runat="server" class="form-title form-control"
        Font-Size="Large" AutoPostBack="True" ForeColor="#888888">
        <asp:ListItem Text="Select" Value="0" Selected="True" />
        <asp:ListItem Text="Male" Value="Male" />
        <asp:ListItem Text="Female" Value="Female" />
    </asp:DropDownList>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="DDLforGender"
        InitialValue="0" runat="server" ValidationGroup="S" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</div>
<asp:Button ID="Button6" runat="server" CssClass="btn btn-success large-text btn-lg"
    OnClick="Button6_Click" Text="SignUp" UseSubmitBehavior="false" ValidationGroup="S" />

Screenshot