Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 744709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:00:35+00:00 2026-05-14T09:00:35+00:00

I populate two dropdownlist in asp.net. Both are assigned to a required field validator.

  • 0

I populate two dropdownlist in asp.net.
Both are assigned to a required field validator.

The codebehind is as below

 if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("emp");
            dt.Columns.Add("ename");
            for (int i = 0; i < 5; i++)
            {
                DataRow dr = dt.NewRow();
                dr["emp"] = (i + 1).ToString();
                dr["ename"] = (i + 1).ToString();
                dt.Rows.Add(dr);
            }
            ddlEmp.DataSource = dt;
            ddlEmp.DataTextField = "emp";
            ddlEmp.DataValueField = "ename";
            ddlEmp.DataBind();
            ListItem l1 = new ListItem("--Select--", "0");
            ddlEmp.Items.Insert(0, l1);
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "emp";
            DropDownList1.DataValueField = "ename";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, l1);

        }

the designer code is as below

 <asp:DropDownList ID="ddlEmp" AutoPostBack="true" runat="server"></asp:DropDownList>
    <asp:RequiredFieldValidator ID="rfvEmp" runat="server" ControlToValidate="ddlEmp" ErrorMessage ="employee" InitialValue="0">
    </asp:RequiredFieldValidator>

    <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"></asp:DropDownList>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1" ErrorMessage ="DropDownList1" InitialValue="0">
    </asp:RequiredFieldValidator>
    <asp:Button ID="btn" runat="server" CausesValidation="true" />

Now what happens is when I choose a field, and then once again go and choose”– Select–“, the validator appears and disappears.

Why doesnt the validator stay?
Where am I going wrong?

Hema

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T09:00:36+00:00Added an answer on May 14, 2026 at 9:00 am

    This issue has bitten me a bunch of times and just because it’s a little wacky how they designed it in my opinion.

    The problem is your using the InitialValue property to compare to the value property of the list item when it should be comparing to the text value. They should have named the property InitialText or something…

    Change your RequiredFieldValidator to the following:

    <asp:RequiredFieldValidator ID="rfvEmp" runat="server" ControlToValidate="ddlEmp" ErrorMessage="employee" InitialValue="--Select--">
    </asp:RequiredFieldValidator>
    
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1" ErrorMessage ="DropDownList1" InitialValue="--Select--">  
    </asp:RequiredFieldValidator>
    

    The client side code is comparing the value that is being displayed, not the value attached to the selection behind the scenes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WebForms page that has two DropDownList controls on it that both
I have two dropdown lists: <asp:DropDownList ID=Field_Type runat=server /> <asp:DropDownList ID=Field_SubType runat=server /> Field_Type
I am building a ASP.NET page which is an Sales Order Processing Form I
We're working on a big ASP.NET\VB.NET website project. I need to populate three dropdownlists.
I have an ASP.net table. In several cells I have two dropdown lists. The
I have an asp page (Default.aspx) that displays the diff between two text files.
I have two divs. I would like to move/populate the text from div id
Suppose I have two .NET DataTables populated as follows, where ID is the primary
I have a webpage with two radiobuttons and a dropdownlist as follows: <div class=sectionheader>Course
I have a web page with various controls. Two of them are dropdownlists. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.