DataGrid Delete Problem

T

thebison

Hi all,

I hope someone can help with this relatively simple problem.
I am building a timesheet application using ASP.NET C# with Visual
Studio 2003.As it is only a protoype application, my database has been
made in MSDE.

I have a DataGrid, and have inserted a delete link button into it
through Property Builder. My Delete method works fine, however I would
like to insert a JScript popup to confirm, as otherwise users can just
delete from the database. With my normal buttons, such as a 'submit'
button on a form, I can just use the following code:

btnSubmit.Attributes.Add("onclick","javascript:if(confirm('Create new
Department - are you sure?')== false) return false;");

However with the DataGrid, the Delete link is created through the
DataGrid, and I do not know how to assign JavaScript to it. I'm sure
someone else must've had this problem before, as no-one wants users to
be able to just delete records so freely! :)

I have already tried following this guide, but can't get it to work..

http://www.dotnetjunkies.com/HowTo/1E7FEE4A-795C-4D33-A135-843EB07C94A8.dcik

Any suggestions on how anyone has achieved this would be much
appreciated!

Al
 
M

Michael Kolias

You can add that attribute by implementing the ItemCreated Event of the
datagrid.
Here is some sample VB.NET code. I am pretty sure you can easily rewrite
this to C#

Public Sub yourdatagrid_ItemCreated(ByVal sender As Object, e as
DataGridEventArgs) Handles yourdatagrid.ItemCreated

If (e.Item.ItemType = ListItemType.Item Or e.ItemType =
ListItemType.AlternatingItem)
Dim btn As Button = Nothing
btn = CType(e.Item.Cells(cell_index).Controls(0), Button)

btn.Attributes.Add("onclick","javascript:if(confirm('Create new
Department - are you sure?')== false) return false;")
End If

If (e.Item.ItemType = ListItemType.EditItem)
Dim btn As Button = Nothing
btn = CType(e.Item.Cells(cell_index).Controls(0), Button)

btn.Attributes.Add("onclick","javascript:if(confirm('Create new
Department - are you sure?')== false) return false;")
End If

End Sub

You have to do this both for the Item/AlternatingItem and EditItem because
when you are in edit mode the cell index will be different.

Hope this helps.
 

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
474,269
Messages
2,571,097
Members
48,773
Latest member
Kaybee

Latest Threads

Top