我有一个带有硬编码年份的用户定义控件,但我需要动态更新它以从给定年份加上 future 七年开始。这最好用 JS 完成吗?
<asp:DropDownList ID="ddlYear" runat="server" DefaultValue="0">
<asp:ListItem Text="" Value="0"></asp:ListItem>
<asp:ListItem Text="2018" Value="2018"></asp:ListItem>
<asp:ListItem Text="2019" Value="2019"></asp:ListItem>
<asp:ListItem Text="2020" Value="2020"></asp:ListItem>
<asp:ListItem Text="2021" Value="2021"></asp:ListItem>
<asp:ListItem Text="2022" Value="2022"></asp:ListItem>
<asp:ListItem Text="2023" Value="2023"></asp:ListItem>
<asp:ListItem Text="2024" Value="2024"></asp:ListItem>
<asp:ListItem Text="2025" Value="2025"></asp:ListItem>
</asp:DropDownList>
最佳答案
您可以在 C# 代码中完成此操作。在您的 page_load 中(当不回发时,仅执行一次),您可以分配您的列表:
for ( int i = 0; i <= 7; i++ )
{
ddlYear.Items.Add((DateTime.Now.Year+i).ToString())
}
和您的表单代码
<asp:DropDownList ID="ddlYear" runat="server" DefaultValue="0">
<asp:ListItem Text="" Value="0"></asp:ListItem>
</asp:DropDownList>
关于javascript - 用户定义的控件,具有动态更新的年份列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48434654/