Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add options to a select during pageload in c#

Tags:

html

c#

asp.net

I am trying to create dropdown for date of birth.

<select id="ddlDOBDay" />
<select id="ddlDOBMonth" />
<select id="ddlDOBYear" />

I want to add options during page load.Is there a way to achieve that in c#?What would be the best way to do this?Unfortunately I cannot use dropdownlist at this place.

like image 221
user3806955 Avatar asked Mar 18 '15 04:03

user3806955


1 Answers

You can use something like this-

<select id="ddlDOBDay" runat="server" />
<select id="ddlDOBMonth" runat="server" />
<select id="ddlDOBYear" runat="server" />

protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            // Add items like below example
            ddlDOBDay.Items.Add(new ListItem("1", "1"));
            .
            .
            ddlDOBDay.Items.Add(new ListItem("31", "31"));

            ddlDOBMonth.Items.Add(new ListItem("January", "1"));
            .
            .
            ddlDOBDay.Items.Add(new ListItem("December", "12"));

            ddlDOBYear.Items.Add(new ListItem("1990", "1990"));
            .
            .
            ddlDOBYear.Items.Add(new ListItem("2015", "2015"));
        }
}
like image 161
Manik Arora Avatar answered Oct 06 '22 01:10

Manik Arora