[研究][ASP.NET]DropDownList預先選今年
2023-03-13
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#
********************************************************************************
Default.aspx
年:<asp:DropDownList ID="DropDownList_Year" runat="server" AutoPostBack="true">
<asp:ListItem>2024</asp:ListItem>
<asp:ListItem>2023</asp:ListItem>
<asp:ListItem>2022</asp:ListItem>
<asp:ListItem>2021</asp:ListItem>
</asp:DropDownList>
|
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 如果是在 DetailsView1 中,找出 DropDownList 控制項
//DropDownList ddlYear = (DropDownList)DetailsView1.FindControl("DropDownList_Year");
// 設定預選值為今年的年份
int currentYear = DateTime.Now.Year-1;
if (currentYear > 2024)
{
currentYear = 2024;
}
DropDownList_Year.SelectedValue = currentYear.ToString();
}
}
|
(完)
沒有留言:
張貼留言