C# 将项目添加到列表中,然后将其放入ListItems
我想在C#中列出一些错误的“列表”,但我不确定如何使用什么类型以及如何填充它(“下面的错误”) 下面是一些我试图实现的伪代码C# 将项目添加到列表中,然后将其放入ListItems,c#,asp.net,C#,Asp.net,我想在C#中列出一些错误的“列表”,但我不确定如何使用什么类型以及如何填充它(“下面的错误”) 下面是一些我试图实现的伪代码 //... protected "some list type here" Errors {get;set;} // some other method would populate this.. protected override void OnLoad(EventArgs e) { var errorList = new Something(); //
//...
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);
}
}
有谁能帮我填补空白,以这种方式创建某种无序的错误列表
我的最终目标是从asp:Login控件中的所有asp:validation控件中获取错误消息,然后以列表格式显示每个错误。我不太确定您想要什么,但按照我的理解,它将如下所示:
protected List<Exception> Exceptions = new List<Exception>();
protected void SomeMethod()
{
try
{
...
}
catch(Exception e)
{
this.Exceptions.Add(e);
}
}
protected List Exceptions=new List();
受保护的void方法()
{
尝试
{
...
}
捕获(例外e)
{
本条。例外情况。添加(e);
}
}
您可以创建一个包含特定于错误的属性的类,然后创建一个强类型列表,如下所示:
public class Error
{
//Properties
}
List<Error> errors = new List<Error>();
if(!Valid())
{
errors.Add(new Error {Name = "Error", Description="Description"};
}
公共类错误
{
//性质
}
列表错误=新列表();
如果(!Valid())
{
添加(新错误{Name=“Error”,Description=“Description”};
}
如果您希望我纠正某些问题,请发表评论!