-1

I want to make registration page in which i'll be giving Date of Birth as option.following is the Dropdown list code that i have used to select day.i want to make it as short as possible.kindly suggest.

<asp:DropDownList ID="days" runat="server" AutoPostBack="True" 
 OnSelectedIndexChanged="days_SelectedIndexChanged" >

   <asp:ListItem>1</asp:ListItem>
   <asp:ListItem>2</asp:ListItem>
   <asp:ListItem>3</asp:ListItem>
   <asp:ListItem>4</asp:ListItem>
   <asp:ListItem>5</asp:ListItem>
   <asp:ListItem>6</asp:ListItem>
   <asp:ListItem>7</asp:ListItem>
   <asp:ListItem>8</asp:ListItem>
   <asp:ListItem>9</asp:ListItem>
   <asp:ListItem>10</asp:ListItem>
   <asp:ListItem>11</asp:ListItem>
   <asp:ListItem>12</asp:ListItem>
   <asp:ListItem>13</asp:ListItem>
   <asp:ListItem>14</asp:ListItem>
   <asp:ListItem>15</asp:ListItem>
   <asp:ListItem>16</asp:ListItem>
   <asp:ListItem>17</asp:ListItem>
   <asp:ListItem>18</asp:ListItem>
   <asp:ListItem>19</asp:ListItem>
   <asp:ListItem>20</asp:ListItem>
   <asp:ListItem>21</asp:ListItem>
   <asp:ListItem>22</asp:ListItem>
   <asp:ListItem>23</asp:ListItem>
   <asp:ListItem>24</asp:ListItem>
   <asp:ListItem>25</asp:ListItem>
   <asp:ListItem>26</asp:ListItem>
   <asp:ListItem>27</asp:ListItem>
   <asp:ListItem>28</asp:ListItem>
   <asp:ListItem>29</asp:ListItem>
   <asp:ListItem>30</asp:ListItem>
   <asp:ListItem>31</asp:ListItem>
</asp:DropDownList>
vivek
  • 1,595
  • 2
  • 18
  • 35
  • 1
    Possible duplicate of [What is the best way to code up a Month and Year drop down list for ASP.NET?](http://stackoverflow.com/questions/812330/what-is-the-best-way-to-code-up-a-month-and-year-drop-down-list-for-asp-net) – CodeCaster Sep 21 '16 at 10:45
  • Instead of downvoting all the answers, why don't you tell us what you realy want? All answers below are correct and resolve your question. – DerpyNerd Sep 22 '16 at 05:51

3 Answers3

0

Use the below code:

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

void BindDays()
{
    for(int i=1; i<=31; i++)
    {
        days.Items.Add(new ListItem(i.ToString()));
    }
}
vivek
  • 1,595
  • 2
  • 18
  • 35
0

You can use PopulateDay() method of below link

http://www.aspsnippets.com/Articles/Select-Day-Month-and-Year-Date-from-DropDownList-in-ASPNet.aspx

Vicky_Burnwal
  • 951
  • 9
  • 14
  • 1
    It would be better, if you can include some helpful source code from the link itself. What will happen if the link is broken in the future. Your answer will be of no use in that case. – vivek Sep 21 '16 at 09:45
-1

A dropdown with 31 days will always have 31 options in the end, if you don't want to populate it manually, use javascript/jquery or code-behind.

Examples

For javascript: see here

var min = 1,
    max = 31,
    select = document.getElementById('selectElementId');

for (var i = min; i<=max; i++){
    var opt = document.createElement('option');
    opt.value = i;
    opt.innerHTML = i;
    select.appendChild(opt);
}

For c#: see here You should come up with something like this:

for (int i = 1; i < 32; i++)
{
    days.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
Community
  • 1
  • 1
DerpyNerd
  • 4,743
  • 7
  • 41
  • 92