Click here to check my latest exciting videos on youtube
Search Mallstuffs

Flag Counter
Spirituality, Knowledge and Entertainment


Locations of visitors to this page


Latest Articles


Move to top
Call Codebehind method from GridView ItemTemplate Eval function
Posted By Sarin on Apr 16, 2013     RSS Feeds     Latest Hinduism news
30184 Views

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
Call Codebehind method from GridView ItemTemplate Eval function
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


Note: Images used on this website are either a production of Bhaktivedanta Book Trust(https://www.krishna.com), Iskcon Foundation or were found in google search under "Free to use and share". If any of the images presented here violates copyright issues or infringes anyone copyright or are not under "Fair use", then please bring it to our notice. Read Disclaimer for more.

Share this to your friends. One of your friend is waiting for your share.
Related Articles
Advanced Strings Format for numbers
Working with silver light datagrid
Change color of grid view row or cell dynamically
Commonly used string Functions
showing row details on button click of silver light datagrid
Different Strings Format for Date Time
Different Strings Format for numbers
Tips and tricks with ajax calendar extender control
Working with Grid control in silverlight
create new controls in grid view at runtime

Post Comment