ASP.NET Repeater: Is it possible to display on the first item one element, but not for the rest of the items?
<asp:Repeater ID="Repeater_RatingQuestions_Individuals" runat="server">
<ItemTemplate>
<asp:Label CssClass="bold center" ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Question")%>' />
<asp:Repeater ID="Repeater_RatingAnswers_Individuals" runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem, "Answers") %>'>
<HeaderTemplate>
<table cellpadding="5" cellspacing="8">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"UserID")%>' />
</td>
<td>
<asp:Label ID="Label开发者_如何学Python4" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Answer") %>' />
</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Value") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
This is my repeater. I have 2 classes:
public class cQuestion
{
public string Question { get; set; }
public List<cAnswer> Answers { get; set; }
}
public class cAnswer
{
public string Answer { get ; set; }
public uint Value { get; set; }
public string UserID { get; set; }
}
Without having to creating some other generic list of an object, would it be possible to display for the first record the UserID, in the nested Repeater, but for the rest of them not show the UserID? If it's not possible then fine I'll just go with my alternate solutions. I just wanted to know if this is possible without having to add new properties or new classes. Thanks in advance.
Sorry if this is off a little (I haven't used Repeaters in a very long time). Have you tried calling a Code Behind function from within the repeater?
<asp:Repeater ID="Repeater_RatingQuestions_Individuals" runat="server">
<ItemTemplate>
<asp:Label CssClass="bold center" ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Question")%>' />
<%# SetShowIDToTrue() %>
<asp:Repeater ID="Repeater_RatingAnswers_Individuals" runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem, "Answers") %>'>
<HeaderTemplate>
<table cellpadding="5" cellspacing="8">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text='<%# ShouldThisBeWrittenOut(DataBinder.Eval(Container.DataItem,"UserID"))%>' />
</td>
<td>
<asp:Label ID="Label4" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Answer") %>' />
</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Value") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Then in your code behind
private bool ShowUID { get; set; }
private int? ShouldThisBeWrittenOut(int UserID)
{
if (ShowUID) {
ShowUID = false;
return UserID;
} else {
return null;
}
}
private void SetShowIDToTrue()
{
ShowUID = true;
}
精彩评论