简体   繁体   English

ASP.NET转发器用户控件未显示数据

[英]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(); repeater.DataBind();

ASP.NET controls do not bind automatically. ASP.NET控件不会自动绑定。

HeaderTemplates, I believe, don't bind data, they only display static content. 我相信HeaderTemplates不会绑定数据,它们只会显示静态内容。 You have to put data to bind in the ItemTemplate or AlternatingItemTemplate . 您必须将数据绑定到ItemTemplateAlternatingItemTemplate

I assume you omitted the DataBind statement from this, that needs to be called too. 我假设您从中省略了DataBind语句,该语句也需要调用。

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(); 并写出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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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