In this article I will explain with an example, how to use the Substring function with EVAL function to strip (cut down) length of long string in ASP.Net using C# and VB.Net.
The Substring function will be used with EVAL function with the help of Ternary operators (? :) in ASP.Net with C# while using IIF function in ASP.Net with VB.Net.
This article is applicable to various controls like GridView, ListView, DataList, Repeater, DetailsView, FormView, etc.
 
 
HTML Markup
The HTML Markup consists of an ASP.Net GridView with two BoundField columns and a TemplateField column.
Inside the TemplateField column, the Substring function is used with EVAL function in order to strip (cut down) length of long string  with the help of Ternary operators (? :)  in C# while using IIF function in VB.Net.
C#
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:TemplateField HeaderText="Description" ItemStyle-Width="150">
            <ItemTemplate>
                <%# (Eval("Description").ToString().Length > 20) ? (Eval("Description").ToString().Substring(0, 20) + "...") : Eval("Description")%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
VB.Net
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:TemplateField HeaderText="Description" ItemStyle-Width="150">
            <ItemTemplate>
                <%# IIf(Eval("Description").ToString().Length > 20, Eval("Description").ToString().Substring(0, 20) + "...", Eval("Description"))%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the ASP.Net GridView control
Inside the Page Load event, the GridView is populated with a dynamic DataTable consisting of some dummy data.
Note: You can learn more about this dynamic DataTable in my article Create DataTable dynamically and bind to GridView in ASP.Net.
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                new DataColumn("Name", typeof(string)),
                new DataColumn("Description",typeof(string)) });
        dt.Rows.Add(1, "John Hammond", "Works as a scientist in USA.");
        dt.Rows.Add(2, "Mudassar Khan", "ASP.Net programmer and consultant in India.");
        dt.Rows.Add(3, "Suzanne Mathews", "Content Writer in France.");
        dt.Rows.Add(4, "Robert Schidner", "Wild life photographer in Russia.");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Description", GetType(String))})
        dt.Rows.Add(1, "John Hammond", "Works as a scientist in USA.")
        dt.Rows.Add(2, "Mudassar Khan", "ASP.Net programmer and consultant in India.")
        dt.Rows.Add(3, "Suzanne Mathews", "Content Writer in France.")
        dt.Rows.Add(4, "Robert Schidner", "Wild life photographer in Russia.")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
 
Screenshot
Using Substring with EVAL function in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads