Using Template Fields With The GridView Control In ASP.NET

One of the simplest ways of displaying databound data on an ASP.NET web page is to add a GridView control. The GridView can be created simply by opening the Database Explorer and dragging the table or view that from which you want to display information. However, the default GridView which is automatically generated in this way almost always needs some tweaking. One typical change you may want to make is to change some of the BoundFields elements to TemplateFields elements.

By default, the GridView displays data using the BoundField object which displays data from a given column in the data source with no real modification. By contrast, any content you desire can be placed inside a TemplateField element. This makes it ideal for setting up validation through the use of validation controls.

TemplateFields may contain static HTML/CSS, ASP.NET web server controls as well as databinding statements. They also offer great flexibility by allowing you to include a variety of templates to cater for the different states of the conditions arising within the GridView. There are several types of template which may be added inside a TemplateField object; the main ones are described below.

The HeaderTemplate provides a way of customizing the information which will be displayed in the header of the column in which the TemplateField is located.

The ItemTemplate is used to contain the information which you want displayed for each row of data when the GridView object is not selected for editing.

The EditItemTemplate is used to display the information which you want displayed for each row of data when the GridView object is in edit mode. It is here that you would place the controls necessary for validation.

Typically, you would display the information currently held in the database by adding a TextBox control inside the EditItemTemplate and databind it to the appropriate column from the data source using a statement like Bind(”FirstName”). Inside the same EditItemTemplate, you would then place the necessary validation control. For example, if you want to ensure that the field is not left blank when the form is submitted, you would insert a RequiredFieldValidator control.

The writer of this article is a trainer and developer with Macresource Computer Training, a UK IT training company offering ASP.NET Classes in London and throughout the UK.

categories: asp.net,web development,web design,internet,computers,software

VN:F [1.8.0_1031]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.0_1031]
Rating: 0 (from 0 votes)
  • Share/Bookmark
Similar Posts:
The ASP.NET GridView Control Offers Developers Speed And Flexibility
The Use Of ASP.Net 3.5 DataBound Controls
Car Template 1 | Free Wordpress Themes – Widget Ready
Template #24315 from WordPress Themes | Web Site Templates …
Building A Template In Adobe Dreamweaver
EUFreelance.com – Customize an existing Wordpress template
Template #24479 from WordPress Themes | Web Site Templates …

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv Enabled
Tags:
Separate individual tags by commas