javascript confirm

  • Thread starter Thread starter mark
  • Start date Start date
M

mark

im having issues in creating a javascript confirm option on pressing a
delete button in a datagrid - ive tried a few things but nothing seems to
work (either crashes or does nothing)
my codebehind is laid out like this :-

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.ItemCommand

'edit from datagrid

If e.CommandSource.commandname = "Edit" Then

(edit code)

'delete from datagrid

If e.CommandSource.commandname = "Delete" Then

(check to see if user really wants to delete the record)

(delete code)

End Sub
 
mark said:
im having issues in creating a javascript confirm option on pressing a
delete button in a datagrid - ive tried a few things but nothing seems to
work (either crashes or does nothing)
my codebehind is laid out like this :-

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.ItemCommand

'edit from datagrid

If e.CommandSource.commandname = "Edit" Then

(edit code)

'delete from datagrid

If e.CommandSource.commandname = "Delete" Then

(check to see if user really wants to delete the record)

(delete code)

End Sub

You can't fire a client-side javascript confirm from a server-side
codebehind handler. The confirm needs to be present in the html
on the client-side.

I don't know how to do it for a DataGrid, but for a regular button
or linkbutton you have to add an html-attribute:

myBtn.Attributes["onclick"] = " if ( ! confirm( 'really delete?' )) return false; "

Note: especially in a linkbutton you don't want "return confirm(..)",
as ASP.Net adds it's own code to the onclick handler. That is also
why you need the closing ";".

Hans Kesting
 
Use the DataGrid's ItemCreated event and in its handler you could search for the Delete Button's instance and wire the javascript confirm code. ItemCreated is called once for every
item created in the grid (Item, AlternatingItem and so on). I've done something similar with a DataList control. Code snippet sample follows

private void datCodes_ItemCreated(object sender, System.Web.UI.WebControls.DataListItemEventArgs e

DropDownList ddl = new DropDownList()

if (e.Item.ItemType == ListItemType.Item

//Logged in user can only view user dat
if (si.Modify_Users != "Y"

((Button)(e.Item.FindControl("btnDatItemDelete"))).Visible
= false

els

((Button)(e.Item.FindControl
("btnDatItemDelete"))).Attributes.Ad
("onclick","javascript:if(confirm_request('Delete User Code')
== false) return false;")


and so on..
 
Back
Top