PDA

Click to See Complete Forum and Search --> : alert boxes


Techno
Aug 12th, 2004, 05:02 AM
Hi there.

I am using vb.net and asp.net to make a website

basically, it returns records from a SQL database and im using a datalist to display them in

in this DL there are 2 buttons, delete and update. cool, it works fine

but now i wish to show a confirmation box when the user hits the delete button - how is this done?

crptcblade
Aug 12th, 2004, 06:15 AM
Something like this should do the trick...

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

Techno
Aug 12th, 2004, 07:02 AM
thanks!

im trying to do this but it doesnt work? whats wrong with the syntax?



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! :-/

nemaroller
Aug 12th, 2004, 07:04 AM
Crpt's is the standard method....

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.


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.

http://www.beyerphotography.com/deletediv.jpg

nemaroller
Aug 12th, 2004, 07:07 AM
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 .

Techno
Aug 12th, 2004, 07:17 AM
ok nm i used jscript

but now i want to tell it to do something when ok is clicked, with the vb.net code i have done....

any ideas how?



Dim confirmScript As String

confirmScript = "<Script language=JavaScript>"
confirmScript &= "var confirmBox=confirm(" & Chr(34) & "Are you sure you wish to delete this record? - Record #" + theID.Text.ToString() & Chr(34) & ");"
confirmScript &= "if(confirmBox) "
confirmScript &= "alert(" & Chr(34) & "You clicked ok" & Chr(34) & ");"


SO now if they clicked ok, i want vb.net code to be executed in this method


confirmScript &= " else alert(" & Chr(34) & "You clicked cancel" & Chr(34) & ");"
'confirmScript &= "} "
confirmScript &= "</script>"

If Not (Page.IsStartupScriptRegistered("clientScript")) Then
Page.RegisterStartupScript("alertScriptk", confirmScript)
End If

Techno
Aug 12th, 2004, 07:18 AM
thanks for that guys, i didnt see the other responses! :) but still the post i made before this, that question is still valid

nemaroller
Aug 12th, 2004, 07:37 AM
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.

If you return true, the button does get clicked.



'put in Item_Created sub
b.Attributes.Add("onClick", "return confirmBoxCode()")

'put in Page_Prerender
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
Page.RegisterClientScriptBlock("confirmscript",myscript)


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)

Techno
Aug 12th, 2004, 09:11 AM
hmmm....

still dont understand, im sorry :( i am new to this.

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....

s

nemaroller
Aug 12th, 2004, 02:34 PM
OK... let's step back for a second.

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.


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!')

Techno
Aug 12th, 2004, 03:00 PM
Thanks for replying :)

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.

:) hope it makes sense

nemaroller
Aug 12th, 2004, 03:57 PM
So your main problem is that:

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?

Techno
Aug 12th, 2004, 04:17 PM
hehe -

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.....

get what i mean? :):afrog:

nemaroller
Aug 12th, 2004, 04:45 PM
You don't need to tell it to execute the code.

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.

Techno
Aug 12th, 2004, 04:48 PM
ok ok - im kinda following it better :) thanks!

so hmm..i will try that code 2morrow at work and see how it goes

so generally how should it be?

make a big string with the confirm box and return true in the "if confirm == 1" clause? and then copy and paste the code there?

meh - is there a demo u have i could see if u dont mind? please? :)

nemaroller
Aug 12th, 2004, 04:49 PM
I'll whip one up for you, you will have it by the morning (I realize your in the UK)

Techno
Aug 12th, 2004, 04:52 PM
thanks :)

nemaroller
Aug 12th, 2004, 05:45 PM
Here you go....

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.

Techno
Aug 13th, 2004, 03:27 AM
thanks!

i was re reading the thread, it seems to sort it out using this on its own:


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

nemaroller
Aug 13th, 2004, 06:44 AM
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:

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');")
DirectCast(e.Item.Controls(3), Button).CommandName = "deleteitem"
End If
End Sub

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)