c# - asp : repeater. items.count 始终为零

标签 c# asp.net

我是 ASP 的新手,对此有点困惑。我有一个中继器 ( basicInfoReport ) 链接到数据源 ( basicInfo )。这部分工作正常; Eval中继器中的调用正在返回正确的数据。

我还有一些代码隐藏,旨在设置文本代替 <asp:Literal>在页面上。我在下面放了两者的片段。

据我所知,转发器正在被数据绑定(bind)(因为 Eval 调用正在工作),所以我无法弄清楚为什么它报告零项,因此,foreach语句什么都不做。谁能帮忙? :)

编辑:已解决,请在更下方查看更新的代码

ASP(删除无关内容)

<asp:AccessDataSource ID="basicInfo" runat="server" 
    DataFile="~/Disasters.accdb" 
    SelectCommand="SELECT * FROM [DisasterTable] WHERE ([ID] = ?)">
    <SelectParameters>
        <asp:QueryStringParameter Name="ID" QueryStringField="ID" Type="Int32" />
    </SelectParameters>
</asp:AccessDataSource>

<asp:Repeater ID="basicInfoReport" runat="server" DataSourceID="basicInfo" 
    onitemdatabound="basicInfoReport_ItemDataBound">
<ItemTemplate>
<h2>Disaster report: <%#Eval("Description")#%></h2>
<b><i>This report is confidential.</i></b><br /><br /><br />

This event was reported on <tt><%#Eval("dateReported")#%></tt>. It was reported  <asp:Literal ID="_wasReportedAnonymously" runat="server"></asp:Literal> and is currently  <asp:Literal ID="_isEmergency" runat="server"></asp:Literal> classed as an emergency.
</ItemTemplate>
</asp:Repeater>

C#(已删除无关内容)

protected void basicInfoReport_ItemDataBound(object sender, RepeaterItemEventArgs e) {

// [...]

// reader refers to an OleDbDataReader that is used for some database interaction in this method.
// until this point, basicInfoReport has not been referenced at all.
if (reader[1].ToString().Equals("False"))
   {

     // at this point basicInfoReport.Items.Count is always zero

     foreach (RepeaterItem repeaterItem in basicInfoReport.Items) {

       // control never makes it this far

       if (repeaterItem.ItemType == ListItemType.Item || repeaterItem.ItemType == ListItemType.AlternatingItem)
          {

             Literal emergencyLiteral = (Literal)repeaterItem.FindControl("_isEmergency");

             emergencyLiteral.Text = "not";
           }
      }
   }

 // [...]

 }

更新:感谢 almog.ori 的帮助,解决了这个问题。供我引用,这里是工作代码:

C#(已删除无关内容)

protected void basicInfoReport_ItemDataBound(object sender, RepeaterItemEventArgs e) {

// [...]

// reader refers to an OleDbDataReader that is used for some database interaction in this method.
// until this point, basicInfoReport has not been referenced at all.
if (reader[1].ToString().Equals("False"))
   {

       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
          {

             Literal emergencyLiteral = (Literal)e.Item.FindControl("_isEmergency");

             emergencyLiteral.Text = "not";
           }
      }

 // [...]

 }

最佳答案

您应该查看 ItemDataBound 的 msdn 文档事件,特别注意事件参数的用法。

      void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {

      // This event is raised for the header, the footer, separators, and items.

      // Execute the following logic for Items and Alternating Items.
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {

事件 ItemDataBound 将事件参数中的“行”交给您。所以你可以用它做你喜欢的事。您应该查看 e.Item.DataItem,这将是被绑定(bind)项目的数据项

关于c# - asp : repeater. items.count 始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5848555/

相关文章:

c# - 如何获取从中提取数据的服务器的服务器地址

c# - 在 xaml 中作为事件处理程序的静态函数

c# - 在 C# 中使用反射确定参数是否使用 "params"?

c# - 在 ASP.NET 中发送带有嵌入图像的邮件

javascript - 用于动态创建控件的日期选择器

c# - 在 C# 的列表中循环遍历列表

c# - "The binary operator Add is not defined for the types ' System.String ' and ' System.String '."-- 真的吗?

c# - web.api 委托(delegate)处理程序在执行 innerhandler 时给出 404

asp.net - 如果 saveChangesAsync 中的多个添加之一失败,是否添加其他人?

ASP.NET URL 导航