Adding "delete" functionality in DataGrid - without a ButtonColumn

P

PK9

I have a datagrid that brings back a number of records and I want to allow
the user to click a "delete" button in each row of the datagrid to delete
records. I originally used a <asp:ButtonColumn> for this and gave it a
CommandName attribute. This worked great. However, the client wants to have
a popup when they click on the delete button.

So now I'm thinking that I have to create a javaScript function and instead
of using an <asp:ButtonColumn>, I'll have to use an <asp:TemplateColumn> with
an embedded <asp:button> inside of the item template. In this button I'm
imagining I'll be able to do an onclick javascript event handler, but I'm not
sure how to do this.

I'm unsure how to create this type of functionality. The important piece
is being able to pass the data for the entire row, from which the delete
button was clicked, to the javascript function. This was easily done with
the button colum because the _ItemCommand event handler for the datagrid took
the input: DataGridCommandEventArgs, which had the data for the entire row.

Any thoughts? Your help is greatly appreciated.
 
E

Elton Wang

You can still keep the delete button but change deleting
function to collect data and save data to SessionState.
Then popup delete page, Response.Write("<script>open
('delete.aspx')</script>"). In delete page, retrieve data
from SessionState.

HTH,

Elton Wang
 
P

PK9

That was my last option if I couldn't figure out another way. I don't want
to deal with another aspx page and session variables.
I found my answer on the following website. This shows examples to do it
with a buttoncolumn and an itemtemplate column! Great reference!!!
http://authors.aspalliance.com/aldotnet/examples/cd.aspx

Elton Wang said:
You can still keep the delete button but change deleting
function to collect data and save data to SessionState.
Then popup delete page, Response.Write("<script>open
('delete.aspx')</script>"). In delete page, retrieve data
from SessionState.

HTH,

Elton Wang
 
P

PK9

Yes, sorry if I was unclear. That's what I was referring to when I said a
javaScript popup when the user clicks on the delete button.

Thanks,
Paul
 
E

Elton Wang

If that, you don't have to change from ButtonColumn to
ItemTemplate + button.

Suppose your delete button (ButtonColumn) is the first
column in the datagrid, just add some code in
datagrid_ItemCreated event:

switch (e.Item.ItemType)
{
case ListItemType.Header:
case ListItemType.Footer:
case ListItemType.Separator:
case ListItemType.Pager:
return;
default:
break;
}
LinkButton btn = (LinkButton)e.Item.Cells(0).Controls(0);
btn.Attributes.Add("OnClick", "confirm('Are you sure you
want to delete this record?');")

That's it.

HTH,

Elton Wang
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top