Home SWOT Analysis How Strong U R? Articles About Us
Mahatma Gandhi Jawaharlal Nehru Bagat Singh Kamarajar Indira Gandhi

Oh Indian

In remembrance of the Independance day Aug 15, 2007

This site is dedicated to one and all who sincerely participated in freedom struggle. Let us try to fulfill their ambitions!
Skip Navigation Links

ASP.NET - Creating Template Field in Grid View


What is a template Field? A template field is nothing but placing standard controls within the GridView. GridView provides some of the controls which are more or less equivalent to the standard controls like TextBox, Checkbox etc.

But sometimes, we may have to place some other standard control within the GridView for better programming and better user experience. In this time we will be going for a template field. Template field also enables us to place controls in header and footer also. For example, we want to place a control in the footer area of the GridView in order to display the total of a column of want to display the total number of records in the footer. Then I use the template field.

Let us see how to display Total number of records available in the footer of the gridview.

Use the following steps to prepare display the footer in Gridview.

  1. Insert a GridView in a form.
  2. In this example we want to show Total number of records in the footer, we need to make sure to display the Footer of the GridView. By default it is False. Choose the GridView. Press F4 to make the properties window visible.  Make sure to set the ShowFooter to True.
    Set ShowFooter to True
  3. Open the GridView Tasks Menu and choose the first column and convert into a template field as shown below:
    Create a Template Field

  4. Using GridView Tasks menu, choose the Edit Templates menu.
    Edit Templates Menu
  5. From the Combobox, choose Footer Template to insert controls in it.
    Choose Footer Template Menu
  6. Insert a label control lblTotalRecords.
    Add a Total Records Label
  7. Now from the GridView Tasks menu, choose end edit template.
    End Template Editing Menu
  8. Make sure to assign the required datasource to the Gridview property through code. If you don't know how to do this, please refere to Listing of Records in Grid View.
  9. Additionally add the following code in the RowDataBound event of the Gridview. The coding concept is very simple. At the time of rendering the output to the browser, we are picking up the label control and assign the value to it.
        
    Protected Sub gvList_RowDataBound(ByVal sender As Object, _
            ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
                    Handles gvList.RowDataBound
        Select Case e.Row.RowType
            Case DataControlRowType.Footer
                Dim lbl As Label
                lbl = e.Row.FindControl("lblTotalRecords")
    
                lbl.Text = "Total : " & CType(gvList.DataSource, _
                                DataTable).Rows.Count
        End Select
    End Sub
    

  10. Run to view the result, like the one given below:
    Peview of the Template Field results
Comments from Users (1)
laminate flooring 01-Sep-2010 06:41:31 
Laminate flooring is an inexpensive type of flooring that offers the consumer many benefits and is relatively easy to install.
Before getting started here is a pro and a con of laminate flooring. Laminate flooring gives your floor that overall look of wood that can be enjoyed for years to come.
Leave a Reply
Name (Required)  
Mail (will not be published) (required)    
Website
 
Invalid Domain! (http://loveletters.oh-indian.com/blogs/20070912/35/asp-net-creating-template-field-in-gridview.aspx) You are not allowed member of our Sites for link exchange!