如何选中两个日期的两个文本框
本文关键字:两个 文本 日期 何选中 | 更新日期: 2023-09-27 18:37:26
一个有txtDateReceived,另一个有txtVendorPackDate。在插入将添加记录之前,我必须检查 txtDateReceived 是否比 txtVendorPackDate 更糟糕。我尝试使用文本更改事件。
protected void txtVendorPackDate_TextChanged(object sender, EventArgs e)
{
DateTime fromDate = DateTime.MinValue;
DateTime toDate = DateTime.MaxValue;
bool parseResultMin = DateTime.TryParse(txtVendorPackDate.Text, out fromDate);
bool parseResultMax = DateTime.TryParse(txtDateReceived.Text, out toDate);
if (toDate < fromDate)
{
txtVendorPackDate.Text = "";
lblDateExpired.Visible = true;
lblDateExpired.Text = "Selected date is incorrect, please enter correct data.";
txtVendorFatPerc.Focus();
}
double expired = toDate.Subtract(fromDate).TotalDays;
if (expired >= 60)
{
lblDateExpired.Text = "Date Expired " + expired + " days after pack day!!!"
lblDateExpired.Visible = true;
}
}
我如何在不使用控件验证的情况下从客户端做到这一点。
试试这个
if (!parseResultMin || !parseResultMax || toDate < fromDate)
在您的代码中,如果两个日期都无效,则 toDate
和 fromDate
都将DateTime.MinValue
,因此表达式 toDate < fromDate
将不为 true。
您可以使用比较
验证程序控件来检查供应商包日期是否小于接收日期。 如果这两个字段都是必填字段,您还可以使用 RequiredFieldValidator。 我会使用RequiredFieldValidators和CompareValidators的组合。
每个文本框都有一个 RequiredFieldValidator,以确保用户输入一个值。每个文本框都有一个比较验证程序,以确保输入的值是日期类型。一个比较验证器,以确保供应商包日期早于接收日期。