Well, after a week I was able to finally figure out what I did wrong. Here is
my code incase any one else would like to make a template item in the code
behind for a gridview delete button.
Sub BuildDelete(ByVal ctl As Control)
Dim acess As IParserAccessor = ctl
Dim del As ImageButton = New ImageButton
del.TemplateControl = Me
del.ID = "Delete"
AddHandler del.DataBinding, AddressOf DataBindDelete
acess.AddParsedSubObject(del)
End Sub
Sub DataBindDelete(ByVal sender As Object, ByVal args As EventArgs)
Dim button As ImageButton = CType(sender, ImageButton)
button.OnClientClick = "return confirm('Are you sure you want to delete
this record?');"
button.ImageUrl = "~/Images/recycle.gif"
button.CommandName = "Delete"
button.AlternateText = "Delete"
button.ID = "Delete"
End Sub
Private Sub GridView_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles GridView.Init
Dim deletefield As New TemplateField()
deletefield.ItemTemplate = New CompiledBindableTemplateBuilder(AddressOf
BuildDelete, Nothing)
GridView.Columns.Insert(0, deletefield)
End Sub
The key was changing Handles GridView.Load to Handles GricView.Init If there
are any comments, suggestions or thoughts about my code please post them. I’m
always looking to improve my code.
Thanks,
Nathan Rover
"Steven Cheng[MSFT]" wrote:
[color=blue]
> Hi Nathan,
>
> Welcome to ASPNET newsgroup.
> AS for the problem you mentioned, here are some of my understanding and
> suggestion:
>
> First, as for dynamic created GridView Fields(just like the dynamic created
> DataGrid columns in asp.net 1.x), we need to create them every time in
> page's request lifecycle(and preferred place is in page_init event...)
> because the Columns (fields) info is not persisted in ViewState.... like
> some other attributes... So we can first ensure that the dynamic fields
> is correctly created and added into fields collection.
>
> After that, as for dynamic TemplateFields, if you think it's a bit too
> complex creating the template..., I think you can consider creating a
> custom GridView Field instead. We just need to create a custom class
> derived from DataControlField and override some functions ....
>
> #DataControlField Class
>
http://msdn2.microsoft.com/en-us/lib...rols.datacontr
> olfield.aspx
>
> Hope helps. Thanks,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure!
www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
>
>
>
> --------------------
> | Thread-Topic: Dynamic template for a delete button in gridview
> | thread-index: AcX9SllWgJn6hkssT2OZ1u5KGmidww==
> | X-WBNR-Posting-Host: 68.187.140.227
> | From: "=?Utf-8?B?TmF0ZURhd2c=?=" <NRover@NoSpam.websitemakers.com>
> | Subject: Dynamic template for a delete button in gridview
> | Date: Fri, 9 Dec 2005 21:27:02 -0800
> | Lines: 75
> | Message-ID: <19EA4404-CE33-47E7-BE93-37E446C5E2D3@microsoft.com>
> | MIME-Version: 1.0
> | Content-Type: text/plain;
> | charset="Utf-8"
> | Content-Transfer-Encoding: 8bit
> | X-Newsreader: Microsoft CDO for Windows 2000
> | Content-Class: urn:content-classes:message
> | Importance: normal
> | Priority: normal
> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
> | Newsgroups: microsoft.public.dotnet.framework.aspnet
> | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
> | Xref: TK2MSFTNGXA02.phx.gbl
> microsoft.public.dotnet.framework.aspnet:363995
> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
> |
> | I'm reposting this. I'm kinda in a bind untill i get this figured out, so
> if
> | anyone has some input it would sure help me out.
> |
> | Ok, I’ve noticed a few gridview problems floating around the forum.
> Everyone
> | wants to do a java confirmation box when a user clicks the delete button.
> | Fair enough, basic user design rules state that you should always confirm
> a
> | delete action. There is also a consensus that the best way to do this is
> a
> | template item. Many users also want to replace the text link buttons with
> | images. This too is a basic design issue and the consensus on the forums
> is
> | to use a command field, it can also be done with a template item but it
> takes
> | a little more coding. Ok, no problem right? So now I have two command
> field
> | items, one for edit and one for select. In addition I have a template
> item
> | for my delete command. They all work just fine while they are in the
> .aspx
> | but now here is my dilemma. I need to be able to do the same thing but in
> the
> | VB code behind page. I managed to get the command field items working
> like so:
> |
> | Dim selectfield As New CommandField()
> | selectfield.ButtonType = ButtonType.Image
> | selectfield.SelectImageUrl = "~/Images/select.gif"
> | selectfield.ShowSelectButton = "True"
> |
> | GridView.Columns.Add(selectfield)
> |
> |
> | But a template item is proving to be a bit more of a chore than one would
> | think. Here is the code that I have so far:
> |
> |
> | Sub BuildDelete(ByVal ctl As Control)
> | Dim acess As IParserAccessor = ctl
> | Dim del As ImageButton = New ImageButton
> | del.TemplateControl = Me
> | del.ID = "DelBut"
> | AddHandler del.DataBinding, AddressOf DataBindDelete
> | acess.AddParsedSubObject(del)
> | End Sub
> |
> | Sub DataBindDelete(ByVal sender As Object, ByVal args As EventArgs)
> | Dim button As ImageButton = CType(sender, ImageButton)
> | button.OnClientClick = "return confirm('Are you sure you want to
> delete
> | this record?');"
> | button.ImageUrl = "~/Images/recycle.gif"
> | button.CommandName = "Delete"
> | button.AlternateText = "Delete"
> | End Sub
> |
> |
> | The following code section is a work arround for a previous problem I
> had,
> | and I’m not too sure if this could be part of the problem now or if I’
> m just
> | masking what the real problem is. When the page goes through a post back
> | event i.e. column sort, or pageing the delete buttons will disapear but
> the
> | column remains. So, I use this code to remove the old column and
> re-insert
> | delete buttons when the GridView loads.
> |
> |
> | Private Sub GridView_Load(ByVal sender As System.Object, ByVal e As
> | System.EventArgs) Handles GridView.Load
> | Dim deletefield As New TemplateField()
> | deletefield.ItemTemplate = New
> CompiledBindableTemplateBuilder(AddressOf
> | BuildDelete, Nothing)
> |
> | If Page.IsPostBack = True Then
> | GridView.Columns.RemoveAt(0)
> | End If
> |
> | GridView.Columns.Insert(0, deletefield)
> | End Sub
> |
> |
> | I could sure use, and appreciate, any help or suggestions anyone is
> willing
> | to offer.
> |
> | Thanks, Nathan Rover
> |
> |
> |
>
>[/color]