Container.ItemIndex

R

rn5a

Using SqlDataAdapter, I am retrieving records from a SQL Server 2005 DB
table & binding them to a DataList. In the DataList, I want the first
column to display the serial numbers starting from 1. I am doing this
using the following code:

<asp:DataList ID="dList" runat="server">
<HeaderTemplate>
<table border="2">
<tr>
<th>#</th>
<th>Col1</th>
<th>Col2</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lblSNo" Text="<%# Container.ItemIndex + 1 %>"
runat="server"/></td>
<td><asp:Label ID="lblCol1" Text="<%# Container.DataItem("Col1") %>"
runat="server"/></td>
<td><asp:Label ID="lblCol2" Text="<%# Container.DataItem("Col2") %>"
runat="server"/></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>

Assuming that 4 records have been retrieved, the 4 Labels under the
first column for the 4 rows render the text 1, 2, 3 & 4 respectively.
Next I change the first Label code to

<td><asp:Label ID="lblSNo" Text="<%# Container.ItemIndex + 1 %>."
runat="server"/></td>

Note the "." (dot) within the Text property of the Label whose ID is
lblSNo. Now the 4 Labels for the 4 rows under the first column do not
render the text 1, 2, 3 & 4. Instead all the 4 Labels just render the
text "." (without the quotes) in the 4 rows under the first column.
Why?

Of course, I can overcome this trivial problem by placing the "."
immediately after the first Label tag closes i.e. just before the
closing </td> tag like this

<td><asp:Label ID="lblSNo" Text="<%# Container.ItemIndex + 1 %>"
runat="server"/>.</td>

but I did like to know why don't the 4 Labels render the text 1, 2, 3 &
4 when a "." is added within the Text property of the first Label.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top