当前位置:编程学习 > C#/ASP.NET >>

创建控件时出错 -Panel1 "Leyp.SQLServerDAL.SQLHelper"的类型初始值设定项引发异常

创建控件时出错 -Panel1
"Leyp.SQLServerDAL.SQLHelper"的类型初始值设定项引发异常



自定义控件,不知道是什么问题  觉得代码没问题啊!求高手指点。 C# 自定义控件 出错 初始值 异常 C# 自定义控件 类型初始值 异常 --------------------编程问答-------------------- 发代码,特别是构造函数调用初始化控件那部分(你实现的) --------------------编程问答--------------------

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


不管如何写,基本思路就是——不要写在实例化方法里! --------------------编程问答-------------------- 除
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,