Dynamic template for a delete button in gridview

G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top