简体   繁体   中英

Customizing ListView control in ASP.NET

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 />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="contentsLabel" runat="server" Font-Size="Small" ForeColor="#666699" Text='<%# Eval("contents") %>' />
            <br />
            <br /></span>
        </ItemTemplate>

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM