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 3272320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:48:50+00:00 2026-05-17T18:48:50+00:00

I develop user control Date of Birth, which contains 3 DropDownList. I need to

  • 0

I develop user control Date of Birth, which contains 3 DropDownList.
I need to make data binding, but I dunno how.

    public partial class DofControl :   System.Web.UI.UserControl {

        public const int YearsCount = 100;
        int _year;
        Months _month;

        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack ) {
                 Bind();

            }
            if (SessionWrapper.JustInserted)
            {
                Bind();
                SessionWrapper.JustInserted = false;
            }

        }

        public object DataSource {
            get; set;
        }

        private void Bind() {
            int[] years = new int[YearsCount];
            int from = DateTime.Now.Year - 5;
            int to = from - 100;


            for (int i = 0; i < YearsCount; i++) {
                years[i] = to;
                to++;
            }
            ddlYear.DataSource = years;
            ddlYear.DataBind();

            ddlMonth.DataSource = Enum.GetNames(typeof(Months));
            ddlMonth.DataBind();


            _year = Int32.Parse(ddlYear.SelectedValue);
            _month = (Months)Enum.Parse(typeof(Months), ddlMonth.SelectedValue);

            BindDays(_year, _month);
            if (DataSource==null)
            {
                ddlYear.SelectedValue = years.Max().ToString();
                ddlMonth.SelectedValue = Months.January.ToString();
                ddlDay.SelectedValue = "1";
            }
            else
            {
                ddlYear.SelectedValue = Convert.ToDateTime(DataSource).Year.ToString();
                ddlMonth.SelectedValue = Enum.GetName(typeof(Months), Convert.ToDateTime(DataSource).Month);
                ddlDay.SelectedValue = Convert.ToDateTime(DataSource).Day.ToString();
            }
         }


        enum Months { January = 1    //.......    }



//Is this right?
        [Bindable(true,BindingDirection.TwoWay)]
        public DateTime Date {
            private get {
                return DateTime.Parse(string.Format("{0}/{1}/{2}", ddlDay.Text, ddlMonth.Text, ddlYear.Text));
            }
            set
            {
                ddlYear.SelectedValue = value.Year.ToString();
                ddlMonth.SelectedValue = value.Month.ToString();
                ddlDay.SelectedValue = value.Day.ToString();
            }
        }




        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e) {


            _year = int.Parse(ddlYear.SelectedValue);
            _month = (Months)Enum.Parse(typeof(Months), ddlMonth.SelectedValue);
            BindDays(_year, _month);
        }


        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e) {
            _year = int.Parse(ddlYear.SelectedValue);
            _month = (Months)Enum.Parse(typeof(Months), ddlMonth.SelectedValue);
            BindDays(_year, _month);
        }




        public bool IsLeapYear(int year) { //.....  }


        private void BindDays(int year, Months month) {
            List<int> days = new List<int>();
            switch (month) {
                case  Months.January:
                case Months.March:
                    for (int i = 1; i <= 31; i++)
                        days.Add(i);
                    break;
                case Months.February:
               //does not matter
        }

    }

I use DofControl in a page Update.aspx inside asp:DetailsView.

<asp:ObjectDataSource ID="odsOneStudent" runat="server" 
        SelectMethod="GetStudentById"
..////
<asp:DetailsView 
//......

<Fields>

   <asp:BoundField DataField="SurName" HeaderText="SurName" />
   <asp:TemplateField HeaderText="Dof">
       <EditItemTemplate>
        <uc:Dof runat="server" ID="dfDof"  Date='<%#Eval("Dof") %>'  />
                    </EditItemTemplate>
                </asp:TemplateField>
 </Fields>
</asp:DetailsView>

I does not work, I get an error “ddlMonth’ has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value”

Am I miss anything?

update: I get an error: “Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control”

  • 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-17T18:48:50+00:00Added an answer on May 17, 2026 at 6:48 pm

    When you set the SelectedValue of a DropDownList the value must be contained in the list’s items.

    Check where you are setting the SelectedValue that the value you are using is correct and contained in the DropDownList.Items

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

Sidebar

Related Questions

I need to develop a template-like user control, which would accept any arbitrary content,
I need to develop a user interface for inputting something like if x or
I need to develop a application for a user's management in a IT Project.
I am trying to develop a functionality and I need to offer user a
I have a user control I have created that contains a details-view that I
I want to develop page control like book ,means when user drags over page(left
I'm trying to develop a custom tri-state button user control and so far I've
HI, I am trying to develop a form which has some user controls ,
I'm trying to develop a system where users can drag icons (which represent user
I want to leverage the Scala's Actor Framework while I develop the user interface

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.