Solved

Null IEnumerator datasource in CompositeDataBoundControl when event fired from inside

Posted on 2006-11-20
3
593 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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