In this article I will explain with an example, how to call Code Behind method (function) in TemplateField of ASP.Net controls such as GridView, DetailsView, DataList, Repeater, etc. using C# and VB.Net.
 
 
HTML Markup
The following HTML markup consists of an ASP.Net GridView consisting of two BoundField columns and one TemplateField column populated using EVAL function.
Using Inline Expression, the value of the EVAL function is passed to a code behind method (function) i.e. GetStatusString.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="50" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:TemplateField HeaderText="Status" ItemStyle-Width="100">
            <ItemTemplate>
                <asp:Label Text='<%# GetStatusString(Eval("Status").ToString())%>'
                    runat="server" />
            </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
The GridView is populated with a dynamic DataTable with some dummy data inside the Page Load event.
Note: You can learn more about this dynamic DataTable in my article Create DataTable dynamically and bind to GridView in ASP.Net.
 
The method (function) GetStatusString accepts the value of the Eval function as strings and after comparing returns the appropriate string value.
Note: The method (function) to be called in ASPX page must always be either Protected or Public. Private methods (functions) of code behind are not accessible in the ASPX page.
 
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"), new DataColumn("Name"), new DataColumn("Status") });
        dt.Rows.Add(1, "John Hammond", "A");
        dt.Rows.Add(2, "Mudassar Khan", "P");
        dt.Rows.Add(3, "Suzanne Mathews", "P");
        dt.Rows.Add(4, "Robert Schidner", "A");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
protected string GetStatusString(string status)
{
    return status == "A" ? "Absent" : "Present";
}
 
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"), New DataColumn("Name"), New DataColumn("Status")})
        dt.Rows.Add(1, "John Hammond", "A")
        dt.Rows.Add(2, "Mudassar Khan", "P")
        dt.Rows.Add(3, "Suzanne Mathews", "P")
        dt.Rows.Add(4, "Robert Schidner", "A")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
Protected Function GetStatusString(status As String) As String
    Return If(status = "A", "Absent", "Present")
End Function
 
 
Screenshot
Call Code Behind method (function) in TemplateField of ASP.Net GridView using C# and VB.Net
 
 
Demo
 
 
Downloads