tags:

views:

168

answers:

3

i created Web user control. it's property is included ListDictionary; But i can not generate in web uset control. if i try wuc in asp.net page, it gives initialize error:

"Object reference not set to an instance of an object."

WEB USER CONTROL:


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

BUT!!

if i add my web page this web user control. And write below codes:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                WebUserControl2_1.Items.Add("Yusuf", "1");
                WebUserControl2_1.Items.Add("Gulden", "2");
            }
        }

        protected void btn_test_Click(object sender, EventArgs e)
        {
            lbl_test.Text = WebUserControl2_1.OrderClause.Trim();

        }

Object reference not set to an instance of an object.

Error point: WebUserControl2_1.Items.Add("Yusuf", "1");

i need listdictionary new method in web user control but how?

+2  A: 

You are referencing the Items property without first instantiating the field. So asking for the item will cause the NullReferenceException. I like the lazy load approach, so when you need it, if it is null, then it will instantiate it.

Inside the getter you could simply put

if(items == null)items = new ListDictionary(); 
return items;

But then you need to think about state management. You have a few options including ViewState and also ControlState.

Andrew

REA_ANDREW
A: 

You're never actually instantiating an instance of the ListDictionary in the items variable In this bit of code:

 private ListDictionary items;
 public ListDictionary Items
    {
        get { return items; }
        set { items = value; }
    }

Change it to something like this:

private ListDictionary items;
public ListDictionary Items
    {
        get { if (items == null) {items = new ListDictionary}; return items; }
        set { items = value; }
    }
Stephen Wrighton
A: 

You are making a couple o mistakes. In first place, all your properties should have a way to store thier values between postback. an easy way to handle this is through viewstate.

Here is an article that gives more deatailed information about this topic http://www.codeguru.com/csharp/.net/net_asp/controls/article.php/c11971/

In second place if it is a collection it is more likekely you will only need a getter since you will be manipullating collection instead of replacing the entire collectio.

so Your code should look like this:

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

Hope this helps,

Igor Zelaya