473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dynamic template for a delete button in gridview

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
Dec 10 '05 #1
3 13708
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=?=" <NR****@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: <19**********************************@microsoft.co m>
| 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
|
|
|

Dec 12 '05 #2
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:
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=?=" <NR****@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: <19**********************************@microsoft.co m>
| 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
|
|
|

Dec 20 '05 #3
Hello, I appreciate your insight and follow up. I am doing a similar
operation, where I dynamically create template fields for my gridview.
However I am having trouble handleing edit updates. As you pointed out, the
data does not stay in the viewstate, so you must refresh the datasource and
recreate the template fields every time you reload. My problem is that I
don't know how to get the new values that have been entered when in edit
mode since the data essentially disapears when you go back to the server.
Any insight would be very much appreciated as this is yet another headache
in the world of dynamic gridviews.

Jason
Aug 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: NateDawg | last post by:
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...
1
by: JasonK | last post by:
I would like to move the Delete button such that it displays one time in the footer row, rather than on every row. I've seen lots of questions asked on the subject around the net, but no answer...
0
by: Tony Hedge | last post by:
Okay I'm back with another issue ;-) Platform is .NET, VB 2005... I created a templace class that I was successfully able to add as a template column to a GridView control - dynamically!!! So...
2
by: cartmann | last post by:
Hi, I have a gridview with a template column. In the template column i have two commandbuttons. When clicking the buttons I enter the cmd_click event - but how do I read in which row the button...
4
by: beton3000 | last post by:
Hello! I'm building a GridView using code to add template fields, because there is a random number of columns in result set from database. I'm using a class with ITemplate interface for defining...
0
by: Eraser | last post by:
Hi to all .NET guru guys... I have a problem in my delete button inside gridview. How to avoid postback on when i select cancel on confirmation message? But postback is okay on Ok confirmation....
1
by: koren99 | last post by:
Hello, I have a gridview that is filled with a datasource & template fields dynamically. ( that has to be done because i ask the user for a datasource) anyway, i started out creating a static...
0
by: Craig Buchanan | last post by:
I am adding templates to a gridview dynamically. these columns are based on data values that are generated prior to calling the gridviews databind. the challenge is when the form does a...
1
by: abhi83 | last post by:
Hi , I have a problem hope someone can help. I have created a dynamic view which contains a gridview. This gridview contains a check box column in the Template Field with the help of ITemplate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.