Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在web用户控件中添加listdictionary(如何初始化新方法)?_C#_.net_Asp.net - Fatal编程技术网

C# 如何在web用户控件中添加listdictionary(如何初始化新方法)?

C# 如何在web用户控件中添加listdictionary(如何初始化新方法)?,c#,.net,asp.net,C#,.net,Asp.net,我创建了Web用户控件。它的属性包含在ListDictionary中;但我无法生成 在web uset控件中。如果我在asp.net页面中尝试wuc,它会给出初始化错误: “对象引用未设置为对象的实例。” WEB用户控件: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FillList1();

我创建了Web用户控件。它的属性包含在ListDictionary中;但我无法生成 在web uset控件中。如果我在asp.net页面中尝试wuc,它会给出初始化错误:

“对象引用未设置为对象的实例。”

WEB用户控件:


  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillList1();
            }
        }

        public String OrderClause
        {
            get { return Session["selecteditem"].ToString(); }
            set { Session["selecteditem"] = value; }
        }
        private ListDictionary  items;

        public ListDictionary Items
        {
            get { return items; }
            set { items = value; }
        }
         void FillList1()
        {
             foreach (string ky in Items.Keys)
            {
                ListBox1.Items.Add(new ListItem(ky, Items[ky].ToString()));
            }
        }
但是 如果我将网页添加到此web用户控件。并写下以下代码: 对象引用未设置为对象的实例。 错误点:WebUserControl2_1.Items.Add(“Yusuf”,“1”)

我需要listdictionary在web用户控制中的新方法,但如何?

您正在引用Items属性,而没有首先实例化字段。因此,请求该项将导致NullReferenceException。我喜欢延迟加载方法,所以当您需要它时,如果它为null,那么它将实例化它

在getter中,您可以简单地

if(items == null)items = new ListDictionary(); 
return items;
但是你需要考虑国家管理。您有几个选项,包括ViewState和ControlState


Andrew

您从未在items变量中实际实例化ListDictionary的实例 在这段代码中:

 private ListDictionary items;
 public ListDictionary Items
    {
        get { return items; }
        set { items = value; }
    }
将其更改为以下内容:

private ListDictionary items;
public ListDictionary Items
    {
        get { if (items == null) {items = new ListDictionary}; return items; }
        set { items = value; }
    }
public ListDictionary Items        
{            
    get 
    {
        object o = ViewState["Items"];
        if (o == null)
        {
            ViewState["Items"] = new ListDictionary;
            o = ViewState["Items"];
        }
        return (ListDictionary) o;
    }
 }

你犯了几个错误。首先,所有属性都应该有一种在回发之间存储它们的值的方法。处理此问题的简单方法是通过viewstate

这里有一篇文章提供了关于这个主题的更多详细信息

第二,如果它是一个集合,则更可能只需要一个getter,因为您将管理集合而不是替换整个集合

因此,您的代码应该如下所示:

private ListDictionary items;
public ListDictionary Items
    {
        get { if (items == null) {items = new ListDictionary}; return items; }
        set { items = value; }
    }
public ListDictionary Items        
{            
    get 
    {
        object o = ViewState["Items"];
        if (o == null)
        {
            ViewState["Items"] = new ListDictionary;
            o = ViewState["Items"];
        }
        return (ListDictionary) o;
    }
 }

希望这有帮助,

您能指出空引用出现的位置吗?