using System; using System.IO; using System.Drawing; using System.Data; using System.Data.SqlClient; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Configuration; using LpHey.Model;namespace LpHey.Components.Controls { public class UserTypeDropDownList : DropDownList { public UserTypeDropDownList() { this.Items.Add(new ListItem("请选择", "")); foreach (UserType t in LpHey.SQLServerDAL.Factory.getUserTypeDAL().getAllUserType()) { this.Items.Add(new ListItem(t.TypeName, t.TypeID.ToString())); } } } }
不具体谈是什么问题了,说一下程序设计方法。在实例化方法中出错的地方(不一定就在这里,例如可能在getUserTypeDAL方法里)是无法调试的。因此在实例化方法中,仅仅应做简单的数据初始化,不要做复杂一点的业务逻辑处理。你可以写到普通的属性方法调用中,例如public class UserTypeDropDownList : DropDownList { private bool inited = false; public override ListItemCollection Items() { if(!inited) { inited = true; this.Items.Add(new ListItem("请选择", "")); foreach (UserType t in LpHey.SQLServerDAL.Factory.getUserTypeDAL().getAllUserType()) { this.Items.Add(new ListItem(t.TypeName, t.TypeID.ToString())); } } } }
少写了一行 public class UserTypeDropDownList : DropDownList { private bool inited = false; public override ListItemCollection Items() { if(!inited) { inited = true; this.Items.Add(new ListItem("请选择", "")); foreach (UserType t in LpHey.SQLServerDAL.Factory.getUserTypeDAL().getAllUserType()) { this.Items.Add(new ListItem(t.TypeName, t.TypeID.ToString())); } } return base.Items; } }不管如何写,基本思路就是——不要写在实例化方法里!
using System.IO;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Configuration;
using LpHey.Model;namespace LpHey.Components.Controls
{
public class UserTypeDropDownList : DropDownList
{
public UserTypeDropDownList()
{
this.Items.Add(new ListItem("请选择", ""));
foreach (UserType t in LpHey.SQLServerDAL.Factory.getUserTypeDAL().getAllUserType())
{
this.Items.Add(new ListItem(t.TypeName, t.TypeID.ToString()));
}
}
}
}
{
private bool inited = false;
public override ListItemCollection Items()
{
if(!inited)
{
inited = true;
this.Items.Add(new ListItem("请选择", ""));
foreach (UserType t in LpHey.SQLServerDAL.Factory.getUserTypeDAL().getAllUserType())
{
this.Items.Add(new ListItem(t.TypeName, t.TypeID.ToString()));
}
}
}
}
{
private bool inited = false;
public override ListItemCollection Items()
{
if(!inited)
{
inited = true;
this.Items.Add(new ListItem("请选择", ""));
foreach (UserType t in LpHey.SQLServerDAL.Factory.getUserTypeDAL().getAllUserType())
{
this.Items.Add(new ListItem(t.TypeName, t.TypeID.ToString()));
}
}
return base.Items;
}
}不管如何写,基本思路就是——不要写在实例化方法里!