创建控件时出错 -Panel1
"Leyp.SQLServerDAL.SQLHelper"的类型初始值设定项引发异常自定义控件,不知道是什么问题  觉得代码没问题啊!求高手指点。C# 自定义控件 出错 初始值 异常C#自定义控件类型初始值异常

解决方案 »

  1.   

    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()));
                }
            }
        }
    }
      

  2.   

    不具体谈是什么问题了,说一下程序设计方法。在实例化方法中出错的地方(不一定就在这里,例如可能在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()));
                }
            }
        }
    }
      

  3.   

    少写了一行  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;
        }
    }不管如何写,基本思路就是——不要写在实例化方法里!