2025年6月3日 星期二

[研究]ASP.NET,動態民國年月下拉選單


[研究]ASP.NET,動態民國年月下拉選單

2025-06-03

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

********************************************************************************

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
    Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        民國年:<asp:DropDownList ID="DropDownList_ReplyYear" runat="server" />
        月:<asp:DropDownList ID="DropDownList_ReplyMonth" runat="server" />
    </form>
</body>
</html>



Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 取得現在的西元年份和月份
                int yearNow = DateTime.Now.Year;
                int monthNow = DateTime.Now.Month;

                // 計算民國年
                int rocYearNow = yearNow - 1911;

                // 設定民國年範圍,例如:近 10 年(也可自行調整)
                int startRocYear = 112;
                int endRocYear = rocYearNow;

                DropDownList_ReplyYear.Items.Clear();
                for (int i = startRocYear; i <= endRocYear; i++)
                {
                    ListItem item = new ListItem(i.ToString());
                    if (i == rocYearNow)
                    {
                        item.Selected = true;
                    }
                    DropDownList_ReplyYear.Items.Add(item);
                }

                // 設定月份
                DropDownList_ReplyMonth.Items.Clear();
                for (int i = 1; i <= 12; i++)
                {
                    ListItem item = new ListItem(i.ToString());
                    if (i == monthNow)
                    {
                        item.Selected = true;
                    }
                    DropDownList_ReplyMonth.Items.Add(item);
                }
            }
        }
    }
}

結果



********************************************************************************

年、月第一個選項是 "全部"

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 取得現在的西元年份和月份
                int yearNow = DateTime.Now.Year;
                int monthNow = DateTime.Now.Month;

                // 計算民國年
                int rocYearNow = yearNow - 1911;

                // 設定民國年範圍,例如:近 10 年(也可自行調整)
                int startRocYear = 112;
                int endRocYear = rocYearNow;

                DropDownList_ReplyYear.Items.Clear();
                DropDownList_ReplyYear.Items.Add(new ListItem("全部", "全部")); // 第一個選項

                for (int i = startRocYear; i <= endRocYear; i++)
                {
                    ListItem item = new ListItem(i.ToString());
                    if (i == rocYearNow)
                    {
                        item.Selected = true;
                    }
                    DropDownList_ReplyYear.Items.Add(item);
                }

                // 設定月份
                DropDownList_ReplyMonth.Items.Clear();                
                DropDownList_ReplyMonth.Items.Add(new ListItem("全部", "全部")); // 第一個選項
for (int i = 1; i <= 12; i++) { ListItem item = new ListItem(i.ToString()); if (i == monthNow) { item.Selected = true; } DropDownList_ReplyMonth.Items.Add(item); } } } } }



(完)

相關

沒有留言:

張貼留言