In my webpage i use the following in order filling the listview control
<asp:ListView ID="ListView1" runat="server">
<layouttemplate>
<asp:PlaceHolder id="itemPlaceholder" runat="server" /></layouttemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="Label1" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans1") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans1Visible") %>'></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans2") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans2Visible") %>'></asp:Label>
<br />
<asp:Label ID="Label3" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans3") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans3Visible") %>'></asp:Label>
<br />
<asp:Label ID="Label4" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans4") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans4Visible") %>'></asp:Label>
<br />
<asp:Label ID="Label5" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans5") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans5Visible") %>'></asp:Label>
<br />
<asp:Label ID="Label6" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Ans6") %>' Visible = '<%# DataBinder.Eval(Container.DataItem, "Ans6Visible") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Now i would like to add numbers to the labels before they are rendered.
For example currently the data displayed are like
Tennis
Football
Basketball
Nfl
Nba
Polo
and the output i would like to have is
1. Tennis
2. Football
3. Basketball
4. Nfl
5. Nba
6. Polo
Could i use ListView1_ItemCreated or the ListView1_ItemDataBound event to achieve this? If that is true, could you point me a place to start?
P.S. the list view is filled with
Dt = GetDataTable("SELECT Ans1, Ans2,Ans3,Ans4,Ans5,Ans6, Ans1Visible,Ans2Visible,Ans3Visible,Ans4Visible,Ans5Visible,Ans6Visible, From myTable WHERE CatID ='" & cat & "'")
ListView1.DataSource = Dt
ListView1.DataBind()
I think that josephj1989 is on the correct path, but is missing the table row code that you stated in your comments was required.
The complete solution would be something like this:
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<asp:PlaceHolder id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<ol>
<asp:Literal ID="Literal1" runat="server" Text = '<%# Eval("Ans1","<li>{0}</li>") %>' Visible = '<%# Eval("Ans1Visible") %>' />
<asp:Literal ID="Literal2" runat="server" Text = '<%# Eval("Ans2","<li>{0}</li>") %>' Visible = '<%# Eval("Ans2Visible") %>' />
<asp:Literal ID="Literal3" runat="server" Text = '<%# Eval("Ans3","<li>{0}</li>") %>' Visible = '<%# Eval("Ans3Visible") %>' />
<asp:Literal ID="Literal4" runat="server" Text = '<%# Eval("Ans4","<li>{0}</li>) %>' Visible = '<%# Eval("Ans4Visible") %>' />
</ol>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
If you are wanting to use the OnItemDataBound approach, the following event handler would work for you:
protected void lvData_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
int count = 1;
foreach (Literal lit in e.Item.Controls.OfType<Literal>())
{
if (lit.Visible)
{
lit.Text = String.Format("{0}. {1}", count.ToString(), lit.Text);
count++;
}
}
}