简体   繁体   English

如何在Listview中操作字符串/数据项?

[英]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 : 这是获取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 ....... 我该如何操作“描述”字符串...即从字符串中获取任何前html标签,仅获得前50个字符/ ........

Thanx in advance 提前感谢

in aspx page 在aspx页面

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

in cs 在cs中

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

In aspx page, you can directly take string part as below: 在aspx页面中,您可以直接将字符串部分如下所示:

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

"OR" “要么”

In aspx page and create ItemDataBound event for the ListView 在aspx页面中,为ListView创建ItemDataBound事件

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

In code behind, create ItemDataBound 在后面的代码中,创建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 要剥离HTML标签,请参见此处: http : //www.dotnetperls.com/remove-html-tags

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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