检查

时间:2015-08-07 15:46:51

标签: c# asp.net if-statement

我有一个带有文本框,下拉列表等的表单。如果通过按下保存按钮更新某些内容,我会创建一个注释,例如如果用户更改国家/地区的名称。

if (cust.CountryName.ToString() != ddlCountry.SelectedItem.Text)
{
    Customer.Notes.InsertNote(cust.ID, Company.Current.CompanyID, DateTime.Now, "Country changed from '" + cust.CountryName + "' to '" + ddlCountry.SelectedItem.Text + "'", CurrentUser.UserID, 1);
}

创建一个注释,说明更改的内容,用户以及日期和时间。

我在表单中对不同的字段有很多类似的if语句,但我想为所有不需要特殊注释的字段创建一个注释。对于某些字段,我只想创建一个注释:

Customer.Notes.InsertNote(cust.ID, Company.Current.CompanyID, DateTime.Now, "Customer updated" , CurrentUser.UserID, 1);

究竟改变了什么并不重要。我只是想知道客户表格已更新。

有没有办法检查表单中的其他字段而不为每个字段创建单独的if语句?基本上,如果更新某些内容并且不是任何if语句,请创建“客户更新”注释。因此,当按下保存按钮时,它会运行所有这些if语句,检查是否进行了任何更改。

1 个答案:

答案 0 :(得分:1)

我们在this chat中找到了对这种特殊情况的答案。如果没有触发if语句,则所需的行为是编写一般注释。

添加属性以跟踪字段的更改:

public bool HasChanges { get; set; }

在每个现有if中添加一行,将其设置为true,因为属性已更改(来自原始帖子):

if (cust.CountryName.ToString() != ddlCountry.SelectedItem.Text)
{
    Customer.Notes.InsertNote(cust.ID, Company.Current.CompanyID, DateTime.Now, "Country changed from '" + cust.CountryName + "' to '" + ddlCountry.SelectedItem.Text + "'", CurrentUser.UserID, 1);
    HasChanges = true;
}  

如果尚未触发另一个if语句,则添加最后一个if语句来编写一般注释。

if (!HasChanges)
{
    //Write general note
}