Creating Custom GridView Control
page 3 of 6
by Abdulla Hussein AbdelHaq
Feedback
Average Rating: 
Views (Total / Last 10 Days): 47786/ 52

Implementing Templates Classes

As I said before, the GridView column consists from the set of Template classes (Header, Item, Alternate and Footer). And in our case we need to implement two template classes (Header and Item templates).

Look at listing 8; I will use these two template classes in the constructor of the template field class, and we will create an object from that template field class.

Listing 8

'Header Template Class
NotInheritable Class MyHeaderTemplate
Implements ITemplate
 
Sub InstantiateIn(ByVal owner As Control) Implements ITemplate.InstantiateIn
  Dim chkboxHeader As New CheckBox
  chkboxHeader.ID = "ChkHeader"
  owner.Controls.Add(chkboxHeader)
End Sub
End Class
'Item Template Class
NotInheritable Class MyItemTemplate
Implements ITemplate
Sub InstantiateIn(ByVal owner As Control) Implements ITemplate.InstantiateIn
  Dim chkboxItem As New CheckBox
  chkboxItem.ID = "ChkItem"
  owner.Controls.Add(chkboxItem)
End Sub
End Class

Here we are setting the look and feel of the header and item template field for the checkbox column; as you can see we are setting the vertical and horizontal align for the header and item template.

Listing 9

Class MyTemplateField
  Inherits TemplateField
  Private _owner As TemplateOwner
  Private _header As MyHeaderTemplate
  Private _item As MyItemTemplate
  Public Sub New()
    _owner = New TemplateOwner
    _header = New MyHeaderTemplate
    _item = New MyItemTemplate
    _header.InstantiateIn(_owner)
    Me.HeaderTemplate = _header
    _item.InstantiateIn(_owner)
    Me.ItemTemplate = _item
    Me.HeaderStyle.VerticalAlign = VerticalAlign.Middle
    Me.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
    Me.HeaderStyle.Width = Unit.Percentage(3)
    Me.ItemStyle.VerticalAlign = VerticalAlign.Middle
    Me.ItemStyle.HorizontalAlign = HorizontalAlign.Center
  End Sub
End Class
<ToolboxItem(False)> _
             Public Class TemplateOwner
  Inherits WebControl
End Class

View Entire Article

User Comments

Title: ss   
Name: aa
Date: 2012-10-31 2:45:13 AM
Comment:
sa sa adsdasad asdas
Title: Nice Article   
Name: Nguyen Van Tai
Date: 2010-08-10 12:24:23 PM
Comment:
This article is very nice. Thanks so much!
Title: Re: Build Error   
Name: Abdulla [The Author]
Date: 2010-01-09 5:25:17 PM
Comment:
Hello Rafael,
it seems that you do not have ajax extensions on your pc.
if you are,then download it and add the below dlls

1. System.Design

2. System.Drawing

3. System.Web

4. System.Web.Extensions (this is for AJAX)

5. System.Web.Extensions.Design (This is for AJAX)
Title: build error   
Name: Rafael
Date: 2010-01-08 11:24:32 AM
Comment:
Thanks for sharin good article. I am new to asp.net. I have downloaded your source in Visual studio 2008 and while compiling it is throwing with "script manager is not declared" and 4 warning msgs. Just guide me. I appreciate.

Thanks
Rafael
Title: Developer   
Name: Amit
Date: 2009-12-17 12:44:01 AM
Comment:
This is a very good article. Thanks for posting it and sharing your knowledge.

Amit
Title: Re: Custom Control   
Name: Abdulla [Author]
Date: 2009-11-26 9:32:24 AM
Comment:
Hello Siva,
open the custom control project, build it with zero errors.
and then to add the dll to your toollbox, follow the below instructions:
Right click on the toolbox window then choose "Add Tab."

Type the name you want for the new tab, right click over it, and click "Choose Items."

Click on the Browse button in the ".NET Framework Components" tab, go to the custom GridView project, open the bin folder, and choose MyCustomControl.dll.
Title: Custom Control   
Name: Siva
Date: 2009-11-26 6:23:50 AM
Comment:
I cannot find custom control and custom grid view in my toolbox. Please help me......
Title: Extra feature Needed !! Please help Abdulla   
Name: anji
Date: 2009-10-26 9:31:02 PM
Comment:
Abdulla ,
u r idea works excellent.what if i want to include an extra header row with all checkboxes in it .And click of each check box in each column header will rise an event.

please,can u give to modifications to ur code to include above said functionality and include as a separate article or mail the file to anji.halaharvi@gmail.com
Title: Need code in C#   
Name: DJ
Date: 2009-08-07 6:42:45 AM
Comment:
Hi
Can u convert all code to C#
Title: Re: Can u convert all code to C#   
Name: Abdulla [The Author]
Date: 2009-08-05 2:09:32 PM
Comment:
Well, I am a Visual Basic Developer.
C# or VB it does not matter, the major point is the idea, there are many tools and websites that enable you to convert code between two languages
Title: Can u convert all code to C#   
Name: Aziz
Date: 2009-08-05 10:36:58 AM
Comment:
if you can, please convert to C# your source.
Title: Applications Programmer   
Name: JHG
Date: 2009-07-07 11:18:20 AM
Comment:
Grr. Visual Basic again!
Title: Hello   
Name: Jilani
Date: 2009-07-07 8:48:22 AM
Comment:
This is nice Art
Title: Mr   
Name: Thanigainathan
Date: 2009-07-07 8:26:09 AM
Comment:
Hi,

This article is very nice. Thanks for giving this opportunity to read this. This will be the base to create advanced controls.

Thanks,
Thani
Title: No Website Code RegisterClient   
Name: User
Date: 2009-06-29 6:02:19 PM
Comment:
Abdulla thanks for the great article this was a great learning point. Unfortunetly I've been getting the following errors.

The ScriptManager.RegisterStartupScript line in "Public Overrides Sub Rendercontrol" method doesn't compile. I had to change it to "Page.ClientScript.RegisterClientScriptBlock"

This line in the onRowCreate "DirectCast(e.Row.FindControl("ChkHeader"), CheckBox).Attributes.Add("onclick", _
"ObjJs_" & Me.ID & ".CheckUnCheckRows(this.checked)")" produces an undefined object when the header checkbox is clicked. Have not been able to solve this one.

I also was unable to find the sample site in the download.
Title: SampleWebsite code missing   
Name: Michael
Date: 2009-06-26 10:05:57 AM
Comment:
Hi there, I'm experiencing the same as Mouzam. There doesn"t appear to any Sample Website code files.

Has this issue been resolved yet?

I tried pasting in the error messages but the result is my previous post.
Title: Extremely Helpful   
Name: Faisal Lodhi
Date: 2009-05-21 6:18:17 PM
Comment:
Thank you for this article, it was much easier to read and understand than so many others that explain how to inherit from GridView
Title: SampleWebsite code missing   
Name: Mouzam
Date: 2009-05-01 10:59:21 AM
Comment:
Hi, a nice simple article. But the SampleWebsite source is missing. I used this link to download (http://authors.aspalliance.com/AAbdelHaq/TestingCustomGridView.zip). CustomGrid source is available but SampleWebsite code is not.
Title: Re: Ms   
Name: Abdulla [The Author]
Date: 2009-03-15 3:14:08 PM
Comment:
Are you sure ? I just downloaded the project successfully !
Please try again, and report us if it is still not working.
Title: Ms   
Name: Diana
Date: 2009-03-13 4:55:58 AM
Comment:
Gr8 article....
But unable to download the sample.
Title: Excellent Article   
Name: Cebo
Date: 2008-11-20 2:20:05 PM
Comment:
This article of example project of gridview with checkbox is very well written and deserves 5-stars!

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 2:30:50 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search