开发者

ASP.NET Repeater User Control DataSource

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 datasour开发者_JS百科ce)

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?


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.


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

do this :

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜