Add items to a list, then make them into ListItems
I want to make some kind of "list" of errors in C#, but I'm not sure how what type to use and how to go about populating it ("Errors" below).
here is some pseudo code for what I'm trying to accomplish.
//...
protected "some list type here" Errors {get;set;} // some other method would populate this..
protected override void OnLoad(EventArgs e) {
var errorList = new Something(); // we'll just do this for now
// Validate stuff
// validation fails, so add a new error message to errorList
// repeat until you have a list of errors to loop through
var ul = new ListBox();
foreach (string errors in Errors) {
var li = new ListItem { text = errors };
开发者_开发技巧 ul.Items.Add(li);
}
}
can anyone help me fill in the blanks for creating some kind of unordered list of errors in this fashion?
My ultimate goal is to grab the error messages from all of the asp:validation controls within an asp:Login control then display each error in a list format
I'm not quite sure of what you want, but the way I understand it, it would look like the following:
protected List<Exception> Exceptions = new List<Exception>();
protected void SomeMethod()
{
try
{
...
}
catch(Exception e)
{
this.Exceptions.Add(e);
}
}
You could create and Error class with properties specific to Errors and then you can create a strongly-typed List like this:
public class Error
{
//Properties
}
List<Error> errors = new List<Error>();
if(!Valid())
{
errors.Add(new Error {Name = "Error", Description="Description"};
}
精彩评论