472,786 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,786 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 13633
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.