c# - asp.net 和 c# 在下拉列表中禁用上个月
问题描述
我想在 C# 的下拉列表中禁用前几个月,只有当月份是当前月份时。
例如,如果我今天是 2020 年 9 月,我想禁用从 2020 年 1 月到 2020 年 8 月的选择功能,并且我希望它能够从 2020 年 9 月/10 月/11 月/12 月选择。
请在这件事上给予我帮助
这是我在后端使用的代码:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
DD_Monthbind();
}
}
private void DD_Monthbind()
{
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
for (int i = 1; i < 13; i++)
{
DropDownList1.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
}
}
}
解决方案
正如其他人在评论中暗示的那样,通常最好(至少从可用性的角度来看)省略不需要的下拉值而不是禁用它们:
private void DD_Monthbind()
{
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
int currentMonth = DateTime.Now.Month;
for (int i = 1; i < 13; i++)
{
bool isMonthInPast = i < currentMonth;
if (!isMonthInPast)
DropDownList1.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
}
}
如果你真的想禁用这些值,你可以使用 JavaScript 或 CSS 来做到这一点。例如(这是 jQuery):
$(/* drop-down value selector */).prop('disabled', true);
推荐阅读
- postgresql - 如何使用 perl 插入 tsrange
- time - 将时间长度转换为分钟格式
- c++builder - C++ Builder 10.3 Rio - 现有和工作项目开始在构建时抛出异常
- excel - 使用vba修改csv文件
- php - Laravel如何检查url参数是否在数组中,否则使用通用路由器
- java - 我可以使用 ANY 或 ARRAY 作为 Hibernate 4.3.11 和 H2 1.4.199 的变量 IN 子句的替代方案吗
- javascript - 返回对象数组中具有最大值的键的最简单方法是什么?
- r - 创建具有递增名称的变量并在其中存储值,所有这些都使用循环
- javascript - 如何添加低音过滤器
- ios - 如何使用 SPM 和 Xcode11 将二进制文件与库链接