Reputation: 167
I am using ListView control in ASP.NET to retrieve data from my database. I studied web form codes for my ListView control and figured out that it basically makes labels to show my data retrieved from the database. I wonder how I can manipulate the label. I can easily change the font color of the label, but I cannot make the label to truncate and show "(..more)" if its length goes more than 10 without resorting to C# code. (I could not find the labels in .cs page.) Is there anyway I can manipulate the C# codes of the labels automaticaly generated by ListView control? Pease let me know. Thanks in advance!
Below is my ListView control in C#
<asp:ListView ID="Posts" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<span>
<asp:Label ID="subjectLabel" runat="server" Font-Bold="True" Font-Size="Large" Text='<%# Eval("subject") %>' />
<br />
<asp:Label ID="contentsLabel" runat="server" Font-Size="Small" ForeColor="#666699" Text='<%# Eval("contents") %>' />
<br />
<br /></span>
</ItemTemplate>
Upvotes: 0
Views: 1390
Reputation: 817
in the code behind page u need to bind the listview control with the datatable which u are fetching like,
Posts.Datasource=dt; Posts.Databind();
where dt is the datatable which u are fetching using sql query.can give more explanation once u write the question more elaborately
Upvotes: 0
Reputation: 13275
If you really want to avoid doing it in code, you could always do it in the SQL that provides the data:
SELECT CASE WHEN Len(contents) > 10 THEN Left(contents, 10) + '...' ELSE contents END
FROM YourTable
WHERE whatever
But you can do it in the databinding Eval()
using the ternary operator if you don't want to use code-behind.
Upvotes: 1