Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am beginner in c#,asp.net and i am creating travelogue page where i have to display reduced form of travelogues.i have used a list view where i have binded labels to database.Below is my listview and codebehind


listview
ASP.NET
<asp:ListView ID="ListView2" runat="server"
           onselectedindexchanged="ListView2_SelectedIndexChanged"  OnPagePropertiesChanging="ListEvents_PagePropertiesChanging" >
           <LayoutTemplate >
     <table class="layout display responsive-table">
        <tr >

        </tr>
        <tbody>
           <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
        </tbody>
     </table>
  </LayoutTemplate>

  <ItemTemplate>
     <tr>
           <td class="organisationnumber">
               <asp:Image ID="Image1" runat="server" Width="150px" ImageUrl=<%# Eval("image")%> /></td>
           <td class="organisationname">
               <asp:Label ID="Label5" runat="server" Text=<%# Eval("heading")%> Font-Bold="True"></asp:Label><br />
                State:<asp:Label ID="Label2" runat="server" Text=<%# Eval("State")%> Font-Bold="True"></asp:Label><br />
               <asp:Label ID="Label6" runat="server" Text=<%# Eval("description")%>></asp:Label></td>
           <td class="actions">
              <a href='<%# "traveloguedet.aspx?ImageID=" + Eval("id") %>' class="edit-item" title="Edit">Click Here</a>

           </td>
       </tr>

  </ItemTemplate>
  <EmptyDataTemplate>
           <td id="Td1"  runat="server">Sorry no Travelogue Available</td>
        </EmptyDataTemplate>

  </asp:ListView>


Codebehind:
C#
string query = "select id,heading,description,state,image from travelogue where trid='1'";

        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();

        da.Fill(table);

        DataTable outputTable = table.Clone();

        for (int i = table.Rows.Count - 1; i >= 0; i--)
        {
            outputTable.ImportRow(table.Rows[i]);
        }





        ListView2.DataSource = outputTable;
        ListView2.DataBind();


Here description is a very long paragraph so if i bind that to the label it displays whole para.I want to display only first 2 or 3 lines of the paragraph.Can anyone tell me a way to do it.please help me.god bless you.Thank you in advance
Posted

1 solution

You can truncate the description text like follows:

ASP.NET
<asp:Label id="Label6" runat="server" text='<%# Eval("description") != null ? TruncateText(Eval("description").ToString(), 200) : string.Empty %>'></asp:Label>


TruncateText(Eval("description").ToString(), 200) displays first 200 characters of the description text.

C#
public string TruncateText(string inputText, int length)
{
	if (inputText.Length <= length)
		return inputText;

	var delimiters = new[] { ' ', '.', ',', ':', ';' };
	var index = inputText.LastIndexOfAny(delimiters, length - 3);

	if (index > (length / 2))
		return inputText.Substring(0, index) + "...";

	return inputText.Substring(0, length - 3) + "...";
}
 
Share this answer
 
v2
Comments
faizel s 13-Sep-14 4:22am    
Thank you Dukhabandhu sahoo for helping me.god bless you.tc

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900