Posted by: wajahatabbas | February 1, 2005

JavaScript Pop up Alert on DataGrid

Well Guys,

A very nitty gritty code but i am sure that everyone needs it, especially when you have a Datagrid with some functionality (edit, delete, etc). As methods like delete must be verified , must be remind before the action performed, we need the confirmation of the user, rather deleting the record at the first instance. You must give him atleast a single LifeLine :).

The scenrio is, you want to show a javascript alert before you delete a record. What you have to do is very simple.

You just need to dig into your Datagrid (Itembound event), and add following code.

**********************************************************

Private Sub DealerViewGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DealerViewGrid.ItemDataBound

If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then

Dim deletebutton As Button
deletebutton = e.Item.Cells(8).Controls(0)
deletebutton.Attributes.Add(“onclick”, “javascript:if(confirm(‘Are you sure to delete?’)== false) return false;”)

End If
End Sub

**********************************************************

Like in my case, i have a datagrid delete command button (delete). In the itembound event i take a deletebutton instance as a button, and then i add an attribute with this button, a javascript confirm message, which will open a dialouge box and ask “Are you sure to delete”, if clicks “Yes”. It will perform the desire as written inside the DataGrid_DeleteCommand, and if cancel clicked. It will left as it was. No ACTION.

So this is how you can add an javascript attribute in a DataGrid button.

If you love those who love you, what’s great, its everyone do. Move Ahead Love those who Persecuted You. You deserve to be GREAT. [Extracted from (Passion of the Christ)]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: