I'm building an audit trail in C#, asp.net. On the .aspx page I have several hidden fields which associate themselves with regular fields (i.e. txtFirstName -> firstNameTrackerHiddenField). I have it set up so that when the user inputs/selects data to a field, the hidden field gets a value set to it. Example:
protected void txtFirstName_TextChanged(object sender, EventArgs e)
{
this.FirstNameTrackerHiddenField.Value = "1";
}
Now to build my Audit Log I was thinking of just checking through each hidden field, pulling the ones that aren't null, and appending a string, depending on which field the user has inputed/changed. I could do a bunch of nested if statements but that would be really sloppy. Sorry if I'm not making any sense, I'm still a little new to the coding world and I thought this methodology would be pretty neat and easy to implement. Here's what I have so far in the auditLog method:
protected string auditLogString()
{
string auditLog = this.txtAuditLogReadOnly.Text + System.Environment.NewLine + System.Environment.NewLine +
DateTime.Now.ToString() + "- (Incident saved by: " + Page.User.Identity.Name + ") ";
if (this.FirstNameTrackerHiddenField.Value != string.Empty)
{
auditLog += "- with changes to First Name."
if (this.LastNameTrackerHiddenField.Value != string.Empty)
{
auditLog += "- with changes to Last Name."
}
}
return auditLog;
}
And the list goes on. There's about 50 fields to cycle through, so that's why I was wondering if there was a better way to go about this... Thanks for any input, and please don't be rough with me... :)
A cleaner way to if/else-ing every HiddenField
would be to loop through the controls on your page, check their type and if they're a HiddenField
with a value, do something with the value.
foreach (Control ctrl in this.Controls)
{
if (ctrl is HiddenField)
{
if (!string.IsNullOrEmpty((ctrl as HiddenField).Value))
{
// do something
}
}
}