[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Null IEnumerator datasource in CompositeDataBoundControl when event fired from inside

Posted on 2006-11-20
3
Medium Priority
?
625 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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