2

I have a user control that contains a repeater. It seems that data cannot be assigned to it's DataSource property, I have tried a few different data sources but the following error is displayed: (I have debugged it and there are data items in the datasource)

Object reference not set to an instance of an object

Here's the aspx for the repeater:

<asp:Repeater ID="repeater1" runat="server" >
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
        <ItemTemplate>
            <li>
                <%# Eval("Name") %>
            </li>
        </ItemTemplate>
    <FooterTemplate>
            </ul>
    </FooterTemplate>
</asp:Repeater>

I create an instance of the user control in the code behind of the file I want to display the repeater in - I can't type repeater1.DataSource and assign it in that page so I added an attribute to the user control which takes IEnumerable as the datasource.

Any ideas why this isn't working?

2
  • where do you assign the datasource to the repeater? If you cannot assign it from your page because the repeater control is inside a user control, create a method like SetDataSource in the user control and you pass a datasource to that method from your page and that method will assign it to the repeater. Commented Sep 29, 2011 at 12:20
  • Thanks for your reply - but it gives the same error. Commented Sep 29, 2011 at 12:26

2 Answers 2

3

You should do such things in the DataBind() method like shown below:

public override void DataBind()
{
   this.repeater.DataSource = ...
   this.repeater.DataBind();
   ...
}

So it will bind whilst parent page calling Page.DataBind() as well

Control.DataBind Method:

Use this method to bind data from a source to a server control. This method is commonly used after retrieving a dataset through a database query. Most controls perform data binding automatically, which means that you typically do not need to call this method explicitly.

Sign up to request clarification or add additional context in comments.

3 Comments

The error still occurs even though the data source contains items. The override method doesn't seem to be being called.
I don't understand how it can display this error when the datasource is being set with an object that contains data?
@aspdotnetuser : are you sure that problem is with repeater? Can you enable CLR exceptions in the Debug -> Exceptions VS Menu and run in debug mode (or attach debugger to w3wp.exe process) and see pwerhaps the problem in an other place
2

Try this: Inside the user controls where is the repeater located, in the OnLoad() method

do this :

 repeater1.DataSource = MyIEnumerableProperty;
 repeater1.DataBind();

1 Comment

I tried that but it errors on the line where the DataSource is set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.