简体   繁体   中英

How do i manipulate a string/dataitem in my Listview?

This is how i am showing the string in listview:

<%# Eval("Description")%>

This is the code in a method to get the data for listview :

lstBlog.DataSource = blg;

        lstBlog.DataBind();

How can i manipulate the "Description" string ... ie get only first 50 characters/stripping off any html tags from the string .......

Thanx in advance

in aspx page

<%# CutString(Eval("Description").ToString(),50) %>

in cs

public string CutString(string value , int len)
{
       // ....
}

In aspx page, you can directly take string part as below:

<%# Convert.ToString(Eval("Description")).Substring(0, 50) %>

"OR"

In aspx page and create ItemDataBound event for the ListView

<asp:Label ID="lblDescription" runat="server" Text=""></asp:Label>

In code behind, create ItemDataBound

protected void lvData_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Blog blg = (Blog)e.Item.DataItem;
        Label lblDescription = (Label)e.Item.FindControl("lblDescription");
        lblDescription.Text = blg.Description.Substring(0, 50);
    }
}

To strip HTML tags please see here: http://www.dotnetperls.com/remove-html-tags

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