ASP.NET VALIDATION FOR GENDER NOT WORKING

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
 969  View(s)
Ratings:
Rate this:

I want to be able to validate whether or not, male/female has been selected or if it hasn't been selected. Here's the code I have: 

protected void checkGender()
    {
        var selectedGender = ddlGender.SelectedValue;
 
        if(selectedGender == "-1")
        {
            Label1.Text = "Please select your gender!";
        }
        else
        {
            Label1.Text = "";
        }
    }
<asp:DropDownList ID="ddlGender"runat="server">
  <asp:ListItem Value="-1">Please select...</asp:ListItem>
  <asp:ListItem Value="Male">Male</asp:ListItem>
  <asp:ListItem Value="Female">Female</asp:ListItem>
</asp:DropDownList>

  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: ASP.NET validation for gender not working

    You can use SelectedIndex:

        var selectedGender= ddlGender.SelectedIndex;
        if (selectedGender== 0)
        {
            Label1.Text ="Please select your gender!";
        }
        else
        {
            Label1.Text ="";
        }

      Modified On Apr-07-2018 01:31:02 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!