April 7, 2007...1:02 pm

Caption Template for GridView: It is possible.

Jump to Comments

OnĀ http://msdn2.microsoft.com/en-us/library/aa479300.aspx Dino Esposito shows us how to implement a Caption template for the GridView control. However he uses a regularĀ tr element, not the caption element. Here is how you can implement a real Caption Template for the GridView control.

using System;
using System.Collections;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;    

public class MyGridView : GridView {    

   protected ITemplate _captionTemplate;    

   [PersistenceMode(PersistenceMode.InnerProperty), TemplateInstance(TemplateInstance.Single), Browsable(false)]
   public ITemplate CaptionTemplate { get { return _captionTemplate; } set { _captionTemplate = value; } }    

   protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding) {    

      int numRows = base.CreateChildControls(dataSource, dataBinding);    

      if ((CaptionTemplate != null && string.IsNullOrEmpty(Caption)) && this.Controls.Count != 0) {
         CustomGridViewCaptionRow captionRow = new CustomGridViewCaptionRow();
         CustomGridViewCaptionCell captionCell = new CustomGridViewCaptionCell();    

         CaptionTemplate.InstantiateIn(captionCell);
         captionRow.Controls.Add(captionCell);    

         this.Controls[0].Controls.AddAt(0, captionRow);
      }    

      return numRows;
   }    

   #region Nested Types    

   private class CustomGridViewCaptionRow : GridViewRow {    

      protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Caption; } }    

      public CustomGridViewCaptionRow() : base(-1, -1, DataControlRowType.Header, DataControlRowState.Normal) { }
      protected override void AddAttributesToRender(HtmlTextWriter writer) { }
   }    

   private class CustomGridViewCaptionCell : TableCell {    

      public override void RenderBeginTag(HtmlTextWriter writer) { }
      public override void RenderEndTag(HtmlTextWriter writer) { }
   }    

   #endregion
}

4 Comments

  • Well done.

    How should I implement a “CaptionRow” property so that the template is rendered before Page_Load() is called? CreateChildControls() is called too late. In the original GridView, TopPagerRow is already rendered when Page_Load() is called…

  • Sorry, already found it the solution:

    override OnInit(), render the row and store it in a private fiels. In CreateChildControls() add the field to the Controls collection.

  • I’m a Newbee…

    I’ve converted this code to VB but am not: A) sure where to put it (in a class file? A usercontrol?) and B) How to use it to extend the my Gridview controls.

    Can you please help?

    Thanks

  • hi guys
    i just want to do paging like
    1 2 3 4 5 next
    previous 6 7 8 9 10 next

    i got solution at
    http://amitkumarmca04.blogspot.com/2009/03/custom-paging-in-grid-view-control.html

    but i dont knw how to use this code ,can some one implement this code with simple user interface

    plz do not give link of any site


Leave a Reply