a blog for those who code

Thursday, 1 June 2017

How to Access Property in ASP.Net GridView Header Template

In this post we will be discussing about accessing property of .cs file in GridView Header Template inside .aspx file. Sometimes you want to have dynamic value in your GridView header template and also you have created a Property in .cs file but the real question is how to use it in .aspx file.

When we create a GrideView and add Columns to it, we need to add a TemplateField and add a HeaderText which will be shown on the front-end as shown below. In the below code we have a HeaderText of Data-2017 but we do not want to hard code the year (i.e. 2017) thus we will be using Property to get the Current Year.

<asp:TemplateField HeaderText="Data-2017">
    <asp:Label ID="DataCurrentYear" runat="server" Text='<%# Bind("DataYear") %>'></asp:Label>

The problem is that you cannot directly use the Property in HeaderText of TemplateField. To use it, you need to add a HeaderTemplate inside TemplateField and add an asp label with the required dynamic data as shown below where FiscalYear is the property which supplies current year property.

    <asp:Label ID="CurrentYear" runat="server" Text='<%# "Data-" + this.FiscalYear %>'></asp:Label>
    <asp:Label ID="DataCurrentYear" runat="server" Text='<%# Eval("CurrentYearActual","{0:N0}") %>'></asp:Label>

This is just a basic example on how you can access the property in GridView Header Template. Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment