gpt4 book ai didi

c# - 将多个文本框的相似验证与不同的验证结果文本结合起来

转载 作者:行者123 更新时间:2023-11-30 15:18:18 25 4
gpt4 key购买 nike

我有一份报告,可以通过 StartDate + EndDateBookingIDCardNumber 进行过滤>金额

这些由 2 个日期选择器和 4 个文本框(CardNumber 拆分为 2 个文本框)表示,并且有一个 Search 按钮可返返回告。我在下面的搜索点击代码运行良好,我想知道我是否可以组合部分或全部验证代码以减少代码行数?大多数验证代码类似于 I.E 检查 BookingIDCardNumberAmount 是否作为整数输入。

我知道这个问题接近于代码审查,所以请让我知道是否应该在其他地方提问。

protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
{
lblInputMessage.Visible = false;
DateTime dtStart, dtEnd;
bool isGood = DateTime.TryParse(txtEndDate.Text, out dtEnd);

if (DateTime.TryParse(txtStartDate.Text, out dtStart))
{
if (txtEndDate.Text.Trim() == "")
{
dtEnd = DateTime.Now;
isGood = true;
}
}

if (txtBookingID.Text.Length > 0)
{
int newBookingID;
if (int.TryParse(txtBookingID.Text, out newBookingID))
{
isGood = true;
}

else
{
lblInputMessage.Visible = true;
lblInputMessage.Text = "Please enter a valid BookingID.";
return;
}
}

if (txtAmount.Text.Length > 0)
{
int newAmount;
if (int.TryParse(txtAmount.Text, out newAmount))
{
isGood = true;
}

else
{
lblInputMessage.Visible = true;
lblInputMessage.Text = "Please enter a valid Amount.";
return;
}
}

if (txtCardNumber1.Text.Length > 0 || txtCardNumber2.Text.Length > 0)
{
int newCardNumber1;
int newCardNumber2;
if (int.TryParse(txtCardNumber1.Text, out newCardNumber1) && (int.TryParse(txtCardNumber2.Text, out newCardNumber2)))
{
isGood = true;
}

else
{
lblInputMessage.Visible = true;
lblInputMessage.Text = "Please enter a valid Card Number.";
return;
}
}

if (isGood)
{
if (dtStart > dtEnd)
{
lblInputMessage.Visible = true;
lblInputMessage.Text = "End Date must be greater than Start Date.";
return;
}

lblInputMessage.Visible = false;
LoadGridData(true);
ajaxCollapsiblePanel1.Collapsed = true;
ajaxCollapsiblePanel1.ClientState = "true";
pnlContainer.Visible = true;
}

else
{
lblInputMessage.Visible = true;
lblInputMessage.Text = "Please enter a valid date.";
return;
}
}

最佳答案

您可以修改它以使用 IDataErrorInfo 并执行类似这样的操作。

public string Error
{
get { return String.Empty; }
}

public string this[string columnName]
{
get
{
String errorMessage = String.Empty;
switch (columnName)
{
case "Variable1":
if (String.IsNullOrEmpty(Variable1))
{
errorMessage = "Variable1 is required";
}
break;
case "Variable2":
if (Variable2 < 10)
{
errorMessage = "Variable2 can't be less than 10";
}
break;
}
return errorMessage;
}
}

本教程https://tarundotnet.wordpress.com/2011/03/03/wpf-tutorial-how-to-use-idataerrorinfo-in-wpf/涵盖基础知识。

关于c# - 将多个文本框的相似验证与不同的验证结果文本结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44198360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com