0

I am trying to create a custom control and the render method is shown below.

I get an "Object reference not set to an instance of an object" error on the bulletList.RenderControl(Writer); line.

Any ideas?

    protected override void Render(HtmlTextWriter Writer)
    {
        if (TermSetList != null && TermSetList.Count > 0)
        {
            BulletedList bulletList = new BulletedList();
            bulletList.Click += new BulletedListEventHandler(BulletListItem_Click);
            bulletList.DisplayMode = BulletedListDisplayMode.LinkButton;
            bulletList.CssClass = "tabs";

            foreach (KeyValuePair<String, String> item in TermSetList)
            {
                ListItem listItem = new ListItem();

                listItem.Text = item.Key;
                listItem.Value = item.Value;

                bulletList.Items.Add(listItem);
            }

            if (!this.Page.IsPostBack)
            {
                bulletList.Items[0].Selected = true;
            }

            bulletList.RenderControl(Writer);

            base.Render(Writer);
        }
    }
1
  • 1
    Have you stepped through it yet? Is the Writer object properly instantiated when the Render method receives it? Commented May 27, 2011 at 16:20

1 Answer 1

1

You generally do not want to add controls during render. How your currently doing this will ensure that your click handler never gets called.

My guess as to why you are getting the error is that the control has not been added to the controls collection.

My suggestion is to move the logic to an earlier event, perhaps onload, you would not need to override the Render method then.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.