Solved

Null IEnumerator datasource in CompositeDataBoundControl when event fired from inside

Posted on 2006-11-20
3
598 Views
Last Modified: 2008-04-16
Hey all.
I'm writing a custom control similar to the asp:repeater.
I have the repeater compositedataboundcontrol (BlogRepeater) which instantiates a bunch of compositecontrols (BlogRepeaterItem) in its BlogRepeater.CreateChildControls(IEnumerable datasource, bool dataBinding) method.

Inside the BlogRepeaterItem there is a button which has a CommandName set which should bubble an event up, which I'll catch in the BlogRepeater for setting of Edit/Insert/Display modes.

The problem is that when I click this button, BlogRepeater.CreateChildControls is called, but the dataSource claims to hold 1 object, which is null (and because it's null the argumentexception below is fired)! The databinding and datasource all works fine before clicking this button.

-- Inside BlogRepeater --
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
        {
            int count = 0;
            if (dataSource != null)
            {
                IEnumerator e = dataSource.GetEnumerator();
                while (e.MoveNext())
                {
                    if (e.Current is Blog)
                    {
                        CreateControl((Blog)e.Current, this);
                        count++;
                    }
                    else
                    {
                        throw new ArgumentException("Received an object that is not a Blog");
                    }
                }
            }
            return count;
        }


private void CreateControl(Blog blog, Control container)
        {
            if (ItemTemplate == null)
            {
                throw new NullReferenceException("ItemTemplate must be set");
            }
            BlogRepeaterItem blogRepeaterItem = new BlogRepeaterItem();
            container.Controls.Add(blogRepeaterItem);
            blogRepeaterItem.CssClass = "BlogRepeaterItem";
            blogRepeaterItem.DataItem = blog;
            ItemTemplate.InstantiateIn(blogRepeaterItem);
            blogRepeaterItem.DataBind();
        }
---------------------

BlogRepeaterItem Template:
-----------------
<ItemTemplate>
            <strong><%# Container.DataItem.Title %></strong>
            <pre><%# Container.DataItem.BlogText %></pre>
            <asp:Button runat="server" ID="test" CommandName="blah" />
        </ItemTemplate>
------------------

0
Comment
Question by:Bastett
  • 2
3 Comments
 

Author Comment

by:Bastett
ID: 17985289
I think I just realised something.
My databinding probably isn't working properly.
It works from the first page load (!IsPostBack) but probably is failing after the first PostBack.
I've stuffed something with ViewState? I'm new to these compositecontrols.
0
 

Author Comment

by:Bastett
ID: 17985417
I think I've found the problem, and it's due to some dodgy Microsoft documentation on the CompositeDataBoundControl.
I was ignoring the "dataBound" boolean parameter (as did their example).
Much searching has revealed that on PostBack the dataSource will contain a number of Null objects equal to the number of objects originally in the dataSource. You use this to iterate and recreate the child controls but don't populate the data as this is done from the ViewState.

I'll request to close and refund this topic
0
 
LVL 1

Accepted Solution

by:
kodiakbear earned 0 total points
ID: 18004490
Closed, 250 points refunded.
kb
Experts Exchange Moderator
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question