How to use footer template in gridview


















To accommodate this level of flexibility the GridView offers the TemplateField, which renders using a template. Furthermore, the TemplateField has a variety of templates that can be used to customize the rendering for different situations. For example, the ItemTemplate is used by default to render the cell for each row, but the EditItemTemplate template can be used to customize the interface when editing data.

In the preceding tutorial we saw how to customize the formatting based on the underlying data using the DataBound and RowDataBound event handlers. Another way to customize the formatting based on the underlying data is by calling formatting methods from within a template. We'll look at this technique in this tutorial as well. For this tutorial we will use TemplateFields to customize the appearance of a list of employees.

Specifically, we'll list all of the employees, but will display the employee's first and last names in one column, their hire date in a Calendar control, and a status column that indicates how many days they've been employed at the company.

In reporting scenarios where you need to use TemplateFields to customize the appearance, I find it easiest to start by creating a GridView control that contains just BoundFields first and then to add new TemplateFields or convert the existing BoundFields to TemplateFields as needed. Therefore, let's start this tutorial by adding a GridView to the page through the Designer and binding it to an ObjectDataSource that returns the list of employees.

These steps will create a GridView with BoundFields for each of the employee fields. Open the GridViewTemplateField. To remove these BoundFields you can:. Take a moment to view our progress in a browser. At this point you should see a table with a record for each employee and four columns: one for the employee's last name, one for their first name, one for their title, and one for their hire date.

Currently, each employee's first and last names are displayed in a separate column. It might be nice to combine them into a single column instead.

To accomplish this we need to use a TemplateField. Both approaches net the same result, but personally I like converting BoundFields to TemplateFields when possible because the conversion automatically adds an ItemTemplate and EditItemTemplate with Web controls and databinding syntax to mimic the appearance and functionality of the BoundField.

The benefit is that we'll need to do less work with the TemplateField as the conversion process will have performed some of the work for us. Select the BoundField to convert from the list in the lower left corner and then click the "Convert this field into a TemplateField" link in the bottom right corner. After this change there's no perceptive difference in the Designer. This can be accomplished either by hand or through the Designer. To do it by hand, simply add the appropriate declarative syntax to the ItemTemplate :.

This will display the GridView's template editing interface. In this interface's smart tag is a list of the templates in the GridView.

Since we only have one TemplateField at this point, the only templates listed in the drop-down list are those templates for the FirstName TemplateField along with the EmptyDataTemplate and PagerTemplate. The EmptyDataTemplate template, if specified, is used to render the GridView's output if there are no results in the data bound to the GridView; the PagerTemplate , if specified, is used to render the paging interface for a GridView that supports paging.

We need to change this so that this property is bound to the value of the LastName data field instead. To accomplish this click on the Label control's smart tag and choose the Edit DataBindings option. This will bring up the DataBindings dialog box. From here you can select the property to participate in databinding from the list on the left and choose the field to bind the data to from the drop-down list on the right.

The DataBindings dialog box allows you to indicate whether to perform two-way databinding. Either approach is fine for this tutorial. Two-way databinding becomes important when inserting and editing data. For simply displaying data, however, either approach will work equally well. We'll discuss two-way databinding in detail in future tutorials. Take a moment to view this page through a browser.

After these changes the GridView's declarative markup should look like the following:. Use the FooterTemplate property to specify the custom content displayed in the footer section of a TemplateField object. Define the content by creating a template that specifies how the footer section is rendered. The content can be as simple as plain text or more complex embedding other controls in the template, for example. To programmatically access a control defined in a template, first determine which TableCell object in the data-bound control contains the control.

Next, use the Controls collection of the TableCell object to access the control. You can also use the FindControl method of the TableCell object to find the control, if the control has an ID property specified. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. After running the application,our GridView will bind with empty row like below because there is no records in Employee Master table Comments or Responses.

Login to post response. Working with Footer Template in Gridview. Posted by vishal kumar in ASP. Introduction As we all know that,Footer template is used for Inserting rows to Gridview as well as in database. Footer template comes under TemplateField inside Gridview.

Footer Template will not be seen if there is some data in the Gridview. When table does not has any data,in that caseGridview will not bind any Data,so Footer Template will not display because gridview has no rows. Therefore,Footer Template will be displayed with empty rows. So we have to Hide first row which is empty or has empty record. Objective Working with Footer Template in Gridview.



0コメント

  • 1000 / 1000