简体   繁体   中英

ASP.NET repeater user control not displaying data

I have a user control that contains a repeater. I populate the datasource for the repeater (I have checked it contains data) but the repeater doesn't display any data. The control has been added to the page correctly.

 //aspx for user control
 <asp:Repeater runat="server">
        <HeaderTemplate><table>
        <%# Eval("Name") %>
 </HeaderTemplate>

 //C# for user control
 public object DataSource { get; set; }
 protected void Page_Load(object sender, EventArgs e)
    {
        this.DataSource = DataSet;
        this.DataBind();
    }

//C# (code behind of aspx page where I want to use the repater user control)
repeater.DataSource = DataSet.ToArray();
repeater.DataBind();

Any ideas why this isn't working?

repeater.DataBind();

ASP.NET controls do not bind automatically.

HeaderTemplates, I believe, don't bind data, they only display static content. You have to put data to bind in the ItemTemplate or AlternatingItemTemplate .

I assume you omitted the DataBind statement from this, that needs to be called too.

Try this way :

    <asp:Repeater ID="repeater1" runat="server"> 
            <HeaderTemplate>
                <table class="datatable fullwidthpercent"> 
                   <tr>
                       <td>Name</td>
                   </tr>
            </HeaderTemplate>
            <ItemTemplate>
                   <tr>
                       <td><%# Eval("Name") %></td> 
                   </tr>
               </table>
            </ItemTemplate>
            <FooterTemplate>
               </table>
            </FooterTemplate>
   </asp:Repeater> 

and also write repeater1.DataBind(); after assigning datasource property.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        getdata();
    }

}
public void getdata()
{
    ds = objfun12.display();
    repeat12.DataSource = ds;
     repeat12.DataBind();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM