im trying to do this but it doesnt work? whats wrong with the syntax?
Code:
Dim confirmScript As String
confirmScript = "<Script language=vbScript>"
confirmScript &= "Msgbox(" & Chr(34) & "Are you sure you want to delete record #:" + theID.Text.ToString() + "?" & Chr(34) & ", MsgBoxStyle.YesNo)"
confirmScript &= "</script>"
If Not (Page.IsStartupScriptRegistered("clientScript")) Then
Page.RegisterStartupScript("alertScriptk", confirmScript)
End If
says "cannot use parentheses when calling a sub" but im not! :-/
The other method is to include a <div>, which will appear when the user click's Delete. Either method of course requires JavaScript to be enabled.
VB Code:
b.Attributes.Add("onClick","showDelDiv")
For this method:
1) you need a HtmlGenericControl (div)
2) A client-side button to cancel a delete
3) A server-side button declared withEvents from your page, which is added to the Div on Init.
Techno, crpt gave you a JAvscript method of doing it.
VBscript is only supported in Internet Explorer.... and unless
you never anticipate a user straying from IE, you should use the
code crpt gave you, which is emca-standard javascript .
When you add 'onClick' to a button, it performs the function designated.
The standard method of doing it is to add javascript to a button so that it only returns the event to the browser if the user confirmed Ok. When you use such a method on a submit button, you can return false if the user cancels, in which case, its as if the button was never clicked.
Dim myscript As String = "<script language='javascript'>function confirmBoxCode(){ return confirm('Are you sure you wish to delete this item');}</script>"
If Not Page.IsClientScriptBlockRegistered("confirmscript") Then
The VB.NET code that gets executed is whatever is in your DataList_ItemCommand (different from the DataList_ItemCreated crpt gave) event handler.
That event handler passes a CommandName and CommandArgument which allows you to ascertain which button from the datalist was pushed (provided you gave those buttons a CommandName and CommandArgument property from your Item_Created event , see crpt's code)
ok i have a function in vb.net (method) which deletes a record when the user clicks on a button in the datalist.
I want it when it has clicked on the button, to ask the user if they want to delete. if so, it calls another method which actually does delete the record, otherwise do nothing....
Let's suppose you aren't putting in a confirmation box. So when a user clicks a Delete button in your datalist, its fires a postback to the server to delete the item.
You say you have buttons in your datalist. We can assume one of them is labeled 'Delete', has a commandname of 'deleteitem', and its commandargument is the pk or id of the record to be deleted.
. I also assume you are binding the commandname and commandargument properties from within the Item_DataBound event of your datalist.(IF not, please post back so)
In your VB.NET code behind, you would also have a subroutine that handles button presses in your datalist. Its within this routine, that your code to delete the record is called.
VB Code:
Private Sub DataList1_ItemCommand(Sender As Object, e As System.Web.UI.Webcontrols.Datalisteventargs) _
Handles DataList1.ItemCommand
If e.CommandName = "deleteitem" Then
Dim indexofItemToDelete As Integer = Integer.Parse(e.CommandArgument)
'Call your function that opens the database,
'and deletes the item using the index supplied
DeleteThisItem(indexOfItemToDelete)
End If
End Sub
So the code above runs when a user clicks delete. Do you have that much?
The only change we are making in allowing a confirmation delete is including client-side javascript to simply intercept the event, and pass it along if the user says 'yes, delete that bloke', or consume the event (cancel it), if the user says 'oops, no I didn't mean to delete it!')
Last edited by nemaroller; Aug 12th, 2004 at 02:39 PM.
I do have a method in vb.net which deletes the entry from the DB - cool.
From what i have been reading and doing my research, in order for a confirmation box to appear is to make a big string with javascript or vbscript to ask the user to delete it or not
ok cool i got that
but the question is, in this big long string, if the user clicks ok, there is an if else statement
if confirmbox == ok (or whatever it is) i want it to execute the vb.net method which deletes it. the thing is, the string will not take in the e arguement
and yes that method u have is similar to mine in terms of the method name and params.
mines is something like:
public sub deleteRecord (byval Source as object, Byval e as data.......) -
i cant remember the last argument but i hope u see what it is. as far as i know - it is a big huge syntax keyword LOL....
the code is at work, dont have it with me. shouldve made a copy....
and i use ctype to check or detect the record id in the datalist - which is fine.
When the user clicks Delete, and then clicks OK (to confirm the delete), you also need to run an If-Else statement before you actually delete the item?
well u obviously need to detect what the user clicked on - ok or cancel right?
so thats an if statement...
if confirm==1
or whatever the syntax is - i need to then tell it to execute a method in my vb.net code.
but how do i do that? since the script language is in a big long string - that string contains the code to show the confirmation box - you cant just close it off after the "IF" statement to see if user clicked ok or not.....
If we didn't have the 'onclick' attribute, clicking the Delete button causes the page to postback to server, at which point ASP.NET re-builds your class, and because the reason why the page was posted is because a person clicked something in the datalist, the DataList_ItemCommand event fires.... you don't have to wire it manually, asp.net handles it for you.
You simply need to tell the browser whether or not to fire the event, by handling with the javascript .... if (confirm("you sure?')) { return true;} else {return false;}
If the javascript code returns true, the event gets fired, otherwise it doesn't, and the page doesn't post back.
You see, without our confirm code, the process is this:
User clicked 'Delete'. IE consumes the event. Because its a button, it posts back to the server.
With the confirm code.
User clicked 'Delete'. Javascript either consumes the event (stops it), or passes it along (returns true).
If its passed along then-> IE consumes the event. Posts the page back to the server.
make an empty VB.NET webapplication, delete the Webform vb makes for you, place these files (unzipped of course) in the web directory, and using the Solution Explorer.. click on your project...add existing item... and add WebForm1.
i was re reading the thread, it seems to sort it out using this on its own:
Code:
Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemCreated
For Each c As Control In e.Item.Controls
If c.ID = "cmdDelete" Then
Dim b As Button = CType(c, Button)
b.Attributes.Add("onClick", "return confirm(""Are you sure?"");")
End If
Next
End Sub
Except that snippet wastes more cpu time and resources, by using a for-each, and a ctype. It will also fail if someone ever tries putting in a header or footer template on the front-side.
My suggestion:
VB Code:
Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
Handles DataList1.ItemCreated
If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
DirectCast(e.Item.Controls(3), Button).Attributes.Add("onClick", "return confirm('are you sure');")
Also, if you have no other buttons in a datalist row, you don't have to assign a specific commandname, and then you could remove the 2nd DirectCast in the above code, and also the test for commandname in the Item_Command event (because the delete button is then the only button in the row that raises the Item_command event)
(Note: you still have to do a directcast in the Item_DataBound event to assign the CommandArgument)
Last edited by nemaroller; Aug 13th, 2004 at 06:53 AM.