DialogBox for Delete command in Datagrid

phamkn

New member
Joined
Jun 24, 2003
Messages
4
Please help

I have a web form which has a datagrid contain whole bunch of textboxes and dropdownlists and a delete column bellow:

<asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="Delete"></asp:ButtonColumn>

Can you show me how to write something in C# or Javascript to confirm users that they really want to delete that row in the datagrid. Any help would appreciate.
 
You have to do it in grids ItemCreated event. Ive made it for my dgPhotoGrid:

Code:
       Private Sub dgPhoto_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgPhoto.ItemCreated
            If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
                Dim _butDelete As Button = CType(e.Item.Cells(3).Controls(1), Button)
                _butDelete.Attributes.Add("onClick", "return(confirm(Are you sure to delete this photo?));")
            End If
        End Sub
 
Delete Validation Datgrid

We dont create row by row, but use SQL Tables to bind data to the datagrid. Therefore, I cant find the itemcareated event.

Here is how we load the data

//Load the data into DataGrid

dgTCDetail.DataSource = GetData(int ID);
dgTCDetail.DataBind();
 
In the design window, double-click your data grid; the code window should appear. Select the data grid control from the objects combo list located at the top left of the code window. Next to the objects combo to the right is the methods/events combo. Select the YourDataGrid_ItemCreated event. Heres where you put the code.
 
You have to read a little bit how asp.net events work :D

Belive me that Ill do the same thing with data binding and this event fires for each row (not only that).
 
Please Help to convert this code into C#. Thank you so much

Private Sub dgPhoto_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgPhoto.ItemCreated
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim _butDelete As Button = CType(e.Item.Cells(3).Controls(1), Button)
_butDelete.Attributes.Add("onClick", "return(confirm(Are you sure to delete this photo?));")
End If
End Sub

or

Sub dgPopularFAQs_ItemDataBound(sender as Object, e as DataGridItemEventArgs)
First, make sure were NOT dealing with a Header or Footer row
If e.Item.ItemType <> ListItemType.Header AND _
e.Item.ItemType <> ListItemType.Footer then
Now, reference the LinkButton control that the Delete ButtonColumn
has been rendered to
Dim deleteButton as PushButton = e.Item.Cells(0).Controls(0)

We can now add the onclick event handler
deleteButton.Attributes("onclick") = "javascript:return " & _
"confirm(Are you sure you want to delete FAQ #" & _
DataBinder.Eval(e.Item.DataItem, "FAQID") & "?)"
End If
End Sub
 
Back
Top