Reputation: 7540
I'm fairly new to ASP and I'm a bit stumped by this. I've got a repeater (basicInfoReport
) linked to a datasource (basicInfo
). This part is working fine; the Eval
calls in the repeater are returning the correct data.
I've also got some code-behind that intends to set text in place of the <asp:Literal>
s on the page. I've put snippets of both below.
As far as I can tell, the repeater is being databound (since the Eval
calls are working), so what I can't work out is why it's reporting zero items, and hence, the foreach
statement is doing nothing. Can anyone help? :)
edit: solved, see updated code even further down
ASP (irrelevant stuff removed)
<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# (irrelevant stuff removed)
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";
}
}
}
// [...]
}
Update: Worked it out thanks to almog.ori's help. For my reference, here's the working code:
C# (irrelevant stuff removed)
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";
}
}
// [...]
}
Upvotes: 3
Views: 4166
Reputation: 7889
You should take a look at the msdn documentation for the ItemDataBound event, perticularly note the usage of the event argument.
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) {
The event ItemDataBound hands you the "row" in the eventargs. So you can do what you like with it. You should look at e.Item.DataItem which would be the data item for the item being bound
Upvotes: 2