Call Codebehind method from GridView ItemTemplate Eval function
Introduction
Gridview is the most common control used in an ASP.Net application. Almost, every websites make use of gridview to display tabular data. Usually, this data comes from the database or xml file, which is then bounded to gridview control.
Each column of the data retrieved from the database is bound to fields (columns) of the GridView. This binding of database column with gridview column is done using Eval function. Normally, we directly bind the retrieved data from the database onto the gridview. But sometimes, it is desirable to tweak or modify the data before it is bounded to the grid column.
For Ex: Instead of binding FirstName and Lastname in two separate columns, I would like to bind it in a single column as FullName, which will display FirstName followed by lastName separated by space in between. Plus I would like to append Mr. or Mrs. depending upon the gender of the person.
Though this can be done at the database level, sometimes we need to handle such scenarios at the front end level.
How to Call Codebehind method from GridView ItemTemplate Eval function
Such kind of requirement can be achieved very easily in .Net. Instead of directly binding Eval functions to columns of gridview, Pass the Eval statement as parameters to code behind function and then used this function to modify the data as per your requirement
For Example, Consider the below item template where I have passed two eval function as the parameter to GetFullName function.
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<% #GetFullName(Eval("FirstName"),Eval("LastName"))%> ' />
</ItemTemplate>
</asp:TemplateField>
This function is defined in the codebehind as
protected string GetFullName(object FirstName, object LastName)
{
return "Mr." + Convert.ToString(FirstName) + " " + Convert.ToString(LastName);
}
As you see above, return type is string so that the modified data is returned back to the calling function. Access modifier is protected so as to make this function accessible from aspx page. Also note that the two input parameters are of object type. Setting it as object is necessary since the Eval function is of type object.
Alternatively, you can use the below code to avoid the type casting done at the code behind level.
<asp:Label ID="Label2" runat="server" Text='<% # GetFullName(Convert.ToString(Eval("FirstName")),Convert.ToString(Eval("LastName"))) %> ' />
In this case, your code behind function would be
protected string GetFullName(string FirstName, string LastName)
{
return "Mr." + FirstName + " " + LastName;
}
In case you are not aware, a little tip to give you an idea on how to do formatting in bounded Eval function. Below code formats the date in Day, Date Month Year format
<asp:Label ID="Label1" runat="server" Text='<% # string.Format("{0:ddd, dd MMM yyyy}", Eval("DOJ"))%> ' />
Output
If I run the above code, my output is shown as
Notice the formatted Name and formatted Date of joining column
Conclusion:
In this article, we saw how to customize the data, to be displayed on the grid view control sing ItemTemplate EVAL function. Hope this article helps youJ