5,696,576 members and growing! (19,853 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » Grid Controls     Intermediate License: The Code Project Open License (CPOL)

GridView with Edit Form (Template)

By Lukas Holota

Comfort editing of a GridView row.
Javascript, CSS, HTML, C#, ASP.NET, ADO.NET, Ajax, Dev

Posted: 9 Feb 2008
Updated: 12 Feb 2008
Views: 14,429
Bookmarked: 15 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
1 vote for this Article.
Popularity: 0.00 Rating: 3.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 100.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5

Introduction

When I got my new job, I found out that developers there were still thinking in .NET 1.1, so they used old methods or directly edited HTML in Render methods. One of the controls made in this terrible way was a GridView that used an edit form instead of a classic edit row, and I wanted to make it the new (and I hope, also the right) way.

Background

Let's start designing:

  1. Get an ITemplate (bindable) of the edit form
  2. Insert the template into the row instead of default cells with inputs
  3. Extract the values on postback and use them to update the data source

Points of interest

The control itself is not complicated, so I will not describe all the code, I'll mention only the interesting parts.

  1. I needed a bindable template that can extract the entered values back, so I used the IBindableTemplate and used it as the property.
  2. [PersistenceMode(PersistenceMode.InnerProperty), 
        TemplateContainer(typeof(GridViewRow), 
        System.ComponentModel.BindingDirection.TwoWay)]
    public IBindableTemplate EditItemTemplate
    {
         set { _EditItemTemplate = value; }
         get { return _EditItemTemplate; }
    }

    The interesting thing about this snippet is the attribute. Ordinary ITemplates provide only one way, so we can not extract the inserted values back, that's why I used the IBindableTemplate and declared two way binding direction in the attribute.

  3. And, the second thing is extracting the values:
  4. protected override void ExtractRowValues(
        System.Collections.Specialized.IOrderedDictionary fieldValues, 
        GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
    {
          if (_EnableTemplateEditing)
          {
              // Not working
              // fieldValues = _EditItemTemplate.ExtractValues(row.Cells[0]);
            
              // Working
              IOrderedDictionary dict = _EditItemTemplate.ExtractValues(row.Cells[0]);
              foreach (string s in dict.Keys)
                  fieldValues.Add(s, dict[s]);
          }
          else
               base.ExtractRowValues(fieldValues, row, includeReadOnlyFields, 
                   includePrimaryKey);
    }

    The strange thing is that you can not assign the IOrderedDictionary you get from the template to the fieldValues object even if fieldValues is empty. You have to add the values key by key, as shown.

To enable template editing, don't forget to set the EnableTemplateEditing property to true.

History

  • 2/9/2008 - First version.
  • 2/12/2008 - Download added.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Lukas Holota


- Still studying in Czech Republic
- I've worked with ASP.NET for over two years so far
- Worked with ASP 3.0 three years before (years of waiting for .NET...)
- 5 years expirience with MS SQL Server 2000 and 2005, 2008 seems to be even better...
Occupation: Web Developer
Location: Czech Republic Czech Republic

Other popular ASP.NET Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Generalnot saving changes for mememberblestab1:05 6 Oct '08  
AnswerRe: not saving changes for mememberLukas Holota2:01 6 Oct '08  
GeneralGridView with Edit Form (Template)memberDinesh N Samarathunga18:46 29 May '08  
GeneralRe: GridView with Edit Form (Template)memberLukas Holota22:51 29 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Feb 2008
Editor: Smitha Vijayan
Copyright 2008 by Lukas Holota
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project