Home > DeveloperSection > Forums > ASP.NET validation for gender not working
Samuel Fernandes
Samuel Fernandes

Total Post:156

Points:1096
Posted on    November-28-2014 10:25 PM

 ASP.Net ASP.NET MVC  Validation 
Ratings:


 1 Reply(s)
 595  View(s)
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>



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Supported
Posted on    November-29-2014 6:43 AM

You can use SelectedIndex:

    var selectedGender = ddlGender.SelectedIndex;

    if (selectedGender == 0)

    {

        Label1.Text = "Please select your gender!";

    }

    else

    {

        Label1.Text = "";

    }


Don't want to miss updates? Please click the below button!

Follow MindStick