开发者

ASP.Net Custom Control Render error

开发者 https://www.devze.com 2023-03-08 16:51 出处:网络
I am trying to 开发者_如何学JAVAcreate 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(W

I am trying to 开发者_如何学JAVAcreate 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);
        }
    }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消