Solved

Null IEnumerator datasource in CompositeDataBoundControl when event fired from inside

Posted on 2006-11-20
3
610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

724 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