In this article I will explain with an example, how to call Code Behind method (function) in ASPX page in ASP.Net controls such as GridView, DetailsView, DataList, Repeater, etc. using C# and VB.Net.
 
 

HTML Markup

The HTML Markup contains of:
GridView – For displaying data.

Columns

The GridView consists of two BoundField columns and one TemplateField column.
TemplateField - The TemplateField column consists of ItemTemplate.
ItemTemplate - It consists of a Label and 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="gvCustomers" 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 namespaces.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 

Binding the ASP.Net GridView control

Inside the Page_Load event handler, an object of DataTable is created.
Then, three columns are added to the DataTable Columns collection using the AddRange method.
An Array of objects of type DataColumn is specified which will hold the name and the optional parameter Data Type i.e. the Type of the column.
Once the schema is ready i.e. all the columns are defined, some rows have been added using the Rows.Add method.
Finally, the DataTable is assigned to the DataSource property of the GridView and the GridView is populated.
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");
        gvCustomers.DataSource = dt;
        gvCustomers.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")
        gvCustomers.DataSource = dt
        gvCustomers.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 ASPX page in ASP.Net using C# and VB.Net
 
 

Demo

 
 

Downloads