简体   繁体   English

转发器在哪里获取数据绑定项?

[英]where does the repeater get it data bound items?

I am having a look at databinding for the first time. 我第一次看到数据绑定。 I understand that the databound elements are put in the aspx file between <%# and %>. 我知道数据绑定元素放在<%#和%>之间的aspx文件中。 I also understand that a Repeater class is used. 我也明白使用了Repeater类。 Like so: 像这样:

<asp:Repeater ID="gvEvents" runat="server">
    <ItemTemplate>
        <div class="eventLogItem">
            <h1><%# Eval("Event")%></h1>
            <time><%# Eval("Timestamp")%></time><small><%# Eval("User")%></small>
            <span class="nav">mouseover to view comments</span>
            <textarea disabled="disabled"><%# Eval("Comments") %></textarea>
        </div>
    </ItemTemplate>
    <SeparatorTemplate>
        <hr />
    </SeparatorTemplate>
</asp:Repeater> `

But where would the aspx code get "Event", "Timestamp" and "User" and "Comments"? 但是aspx代码在哪里获得“Event”,“Timestamp”和“User”以及“Comments”? There does not seem to be something clear in the code-behind file. 代码隐藏文件似乎没有明确的东西。 What am I missing? 我错过了什么?

In your code behind you would set the DataSource property of the repeater to some collection of object in which each of those objects contains a property named Event , Timestamp , User , and Comments . 在您的代码中,您可以将转发器的DataSource属性设置为某个对象集合,其中每个对象都包含名为EventTimestampUserComments的属性。 If you don't assign a data source, then there won't be anything for the repeater to display. 如果您没有分配数据源,则转发器不会显示任何内容。 If any of the bound items is missing one of those properties, you'll get an error at runtime. 如果任何绑定项缺少其中一个属性,您将在运行时收到错误。

查看gvEvents.DataSource = ...后面的代码,这就是为转发器类的数据绑定调用数据库的方式

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

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