我对ASP很新,我对此感到有点难过。我有一个转发器(basicInfoReport
)链接到数据源(basicInfo
)。这部分工作正常;转发器中的Eval
次调用正在返回正确的数据。
我还有一些代码隐藏,它打算在页面上设置文本代替<asp:Literal>
。我把两个片段都放在下面。
据我所知,转发器正在被数据绑定(因为Eval
调用正在工作),所以我无法解决的是为什么它报告零项,因此{{1声明什么都不做。有人可以帮忙吗? :)
编辑:已解决,请进一步查看更新后的代码
ASP(删除无关的内容)
foreach
C#(删除无关的内容)
<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>
更新:感谢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"))
{
// 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";
}
}
}
// [...]
}
答案 0 :(得分:2)
您应该查看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向您发送eventargs中的“行”。所以你可以用它做你喜欢的事。您应该查看e.Item.DataItem,它将是绑定项目的数据项