Solved

Null IEnumerator datasource in CompositeDataBoundControl when event fired from inside

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now