Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 有没有办法减少这种特殊的代码?_Asp.net_.net_Date - Fatal编程技术网

Asp.net 有没有办法减少这种特殊的代码?

Asp.net 有没有办法减少这种特殊的代码?,asp.net,.net,date,Asp.net,.net,Date,我想在注册页面中选择出生日期。以下是我用来选择日期的下拉列表代码。我想使其尽可能短。敬请建议 <asp:DropDownList ID="days" runat="server" AutoPostBack="True" OnSelectedIndexChanged="days_SelectedIndexChanged" > <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp

我想在注册页面中选择出生日期。以下是我用来选择日期的下拉列表代码。我想使其尽可能短。敬请建议

<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>

1.
2.
3.
4.
5.
6.
7.
8.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

一个31天的下拉列表最后总是有31个选项,如果您不想手动填充,请使用javascript/jquery或代码隐藏

示例

对于javascript:

var最小值=1,
最大值=31,
select=document.getElementById('selectElementId');
对于(var i=min;i使用以下代码:

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()));
    }
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
BindDays();
}
}
作废天数()
{

对于(int i=1;i您可以使用下面链接的PopulateDay()


如果您能从链接本身中包含一些有用的源代码,那就更好了。如果将来链接断开,会发生什么情况。在这种情况下,您的答案将毫无用处。您可能会重复所有答案,而不是对所有答案投否决票,为什么不告诉我们您真正想要什么?以下所有答案都是正确的,可以解决您的问题.
for (int i = 1; i < 32; i++)
{
    days.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
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()));
    }
}