Results 1 to 20 of 20

Thread: alert boxes

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    alert boxes

    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?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Something like this should do the trick...
    VB Code:
    1. Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemCreated
    2.  
    3.         For Each c As Control In e.Item.Controls
    4.             If c.ID = "cmdDelete" Then
    5.                 Dim b As Button = CType(c, Button)
    6.  
    7.                 b.Attributes.Add("onClick", "return confirm(""Are you sure?"");")
    8.             End If
    9.         Next
    10.  
    11.     End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    thanks!

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

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.

    VB Code:
    1. 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.


  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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 .

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    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?

    Code:
            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

    Code:
            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

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    thanks for that guys, i didnt see the other responses! but still the post i made before this, that question is still valid

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.


    VB Code:
    1. 'put in Item_Created sub
    2. b.Attributes.Add("onClick", "return confirmBoxCode()")
    3.  
    4. 'put in Page_Prerender
    5. Dim myscript As String = "<script language='javascript'>function confirmBoxCode(){ return confirm('Are you sure you wish to delete this item');}</script>"
    6.  
    7. If Not Page.IsClientScriptBlockRegistered("confirmscript") Then
    8. 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)

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    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

  10. #10
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.

    VB Code:
    1. Private Sub DataList1_ItemCommand(Sender As Object, e As System.Web.UI.Webcontrols.Datalisteventargs) _
    2.  Handles DataList1.ItemCommand
    3.  
    4. If e.CommandName = "deleteitem" Then
    5.   Dim indexofItemToDelete As Integer = Integer.Parse(e.CommandArgument)
    6.  
    7.   'Call your function that opens the database,
    8.  'and deletes the item using the index supplied
    9.   DeleteThisItem(indexOfItemToDelete)
    10.  
    11. End If
    12.  
    13. 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.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    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

  12. #12
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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?

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    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?

  14. #14
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    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?

  16. #16
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I'll whip one up for you, you will have it by the morning (I realize your in the UK)

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    thanks

  18. #18
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.
    Attached Files Attached Files

  19. #19

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    thanks!

    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

  20. #20
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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:
    1. Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
    2.  Handles DataList1.ItemCreated
    3.         If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
    4.             DirectCast(e.Item.Controls(3), Button).Attributes.Add("onClick", "return confirm('are you sure');")
    5.             DirectCast(e.Item.Controls(3), Button).CommandName = "deleteitem"
    6.         End If
    7.     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)
    Last edited by nemaroller; Aug 13th, 2004 at 06:53 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width