Results 1 to 15 of 15

Thread: LinkButton in Datalist to delete...verify first?[RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    LinkButton in Datalist to delete...verify first?[RESOLVED]

    I have a datalist and I have a LinkButton to delete. This is my code behind that:

    VB Code:
    1. Sub DataList_DeleteCommand(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
    2.         Dim conn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings().Item("strConn"))
    3.  
    4.         Dim acctno As String = DataList1.DataKeys(e.Item.ItemIndex)
    5.  
    6.         Dim sql As String = "DELETE FROM CustomerRecords " & _
    7.                             "WHERE ConsumerAccountNumber = '" & acctno & "'"
    8.  
    9.         Dim cmd As New SqlCommand(sql, conn)
    10.         Try
    11.             conn.Open()
    12.             cmd.ExecuteNonQuery()
    13.         Catch ex As Exception
    14.             Response.Write(ex.Message)
    15.         Finally
    16.             If Not conn Is Nothing Then
    17.                 conn.Dispose()
    18.             End If
    19.         End Try
    20.         DataList1.EditItemIndex = -1
    21.         DataBinder()
    22.     End Sub

    I was just wondering if there is a way to have a MessageBox pop saying "Do you want to do this: Yes or No"?

    I'm assuming JavaScript or something maybe? let me know. Thanks!
    Last edited by brendalisalowe; May 27th, 2005 at 11:19 AM.
    Brenda

    If it weren't for you guys, where would I be?

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: LinkButton in Datalist to delete...verify first?

    Show us how your DataList is bound to your data source plus the where the delete button is created.

    Basically you add the JavaScript code like so:
    Code:
    deleteButton.Attributes["OnClick"] = "javascript:return confirm('Are you sure you want to delete this gallery entry?')";
    However where you do this depends on how you create the delete button.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    Re: LinkButton in Datalist to delete...verify first?

    Here is part of my datalist where the delete linkbutton is located. Does that help?


    <ITEMTEMPLATE>
    <TABLE width="100%"> <TR>
    <TD width="9%">
    <asp:LinkButton id="lnkEdit" runat="server" CommandName="edit" Text="Edit"></asp:LinkButton></TD>
    <TD width="9%">
    <asp:LinkButton id="lnkDelete" runat="server" CommandName="delete" Text="Delete"></asp:LinkButton></TD>
    </TR>
    </TABLE>
    </ITEMTEMPLATE>
    Brenda

    If it weren't for you guys, where would I be?

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: LinkButton in Datalist to delete...verify first?

    Yep you need to add the client-side onclick attribute in the DataList's ItemDataBound event.

    Code:
        void DataList1_ItemDataBound( object s, DataListItemEventArgs e ) {
            if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem ) {
                LinkButton deleteButton = (LinkButton) e.Item.FindControl("lnkDelete");
        
                deleteButton.Attributes["onclick"] = "javascript:return confirm('Are you sure you want to delete this?')";
            }
        }
    You also need to add to your existing DataList tag the following to ensure the above method is called:
    Code:
    <asp:DataList id="DataList1" OnItemDataBound="DataList1_ItemDataBound">
    Basically what this does is when the DataList is populated it checks all ItemTypes of Item and AlternatingItem for a control with the id lnkDelete and then adds a client-side event to it. You need to check the ItemType as your DataList's Header, Footer and Separators don't contain this delete button.

    Sorry the code is in C# but my VB.NET ain't too hot.

    Let me know if anything isn't clear.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    Re: LinkButton in Datalist to delete...verify first?

    So I don't put the above code inside this:

    VB Code:
    1. Sub DataList_DeleteCommand(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
    2. End Sub

    And I've tried this but it doesn't work. Is it set up right?
    VB Code:
    1. Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As DataListCommandEventArgs) 'Is this part right?
    2.         If (e.Item.ItemType = ListItemType.Item And e.Item.ItemType = ListItemType.AlternatingItem) Then
    3.             Dim deleteButton As LinkButton = e.Item.FindControl("lnkDelete")
    4.             deleteButton.Attributes("OnClick") = "javascript:return confirm('Are you sure you want to delete this entry?')"
    5.         End If
    6.     End Sub

    Also, I am not hot in C# so can anyone else help me out with that? Thanks!
    Brenda

    If it weren't for you guys, where would I be?

  6. #6
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: LinkButton in Datalist to delete...verify first?

    No you still need the DeleteCommand server-side event to actually delete whatever you need to - the method I posted was just to add a client-side JavaScript "are you sure?" message box. That cannot be done server-side so you must attach a client-side event as I have described in the ItemDataBound event.

    I'll have a go at converting! Unless anyone else beats me to it.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  7. #7
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: LinkButton in Datalist to delete...verify first?

    Actually I think you converted it ok now I've read it. Let me know if it works!

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  8. #8
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: LinkButton in Datalist to delete...verify first?

    Did you add the OnItemDataBound="DataList1_ItemDataBound" to the DataList tag?

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    Re: LinkButton in Datalist to delete...verify first?

    But does || mean AND or OR?
    Brenda

    If it weren't for you guys, where would I be?

  10. #10
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: LinkButton in Datalist to delete...verify first?

    Actually it should be an Or not an And in the if statement.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    Re: LinkButton in Datalist to delete...verify first?

    This is the error I am getting:

    Compiler Error Message: BC30408: Method 'Public Sub DataList1_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs)' does not have the same signature as delegate 'Delegate Sub DataListItemEventHandler(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs)'.


    Why?
    Brenda

    If it weren't for you guys, where would I be?

  12. #12
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: LinkButton in Datalist to delete...verify first?

    Ah yes use DataListItemEventArgs instead of DataListCommandEventArgs.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    Re: LinkButton in Datalist to delete...verify first?

    Cool. It works. But one more question. How does it know if you hit Ok or Cancel?
    Brenda

    If it weren't for you guys, where would I be?

  14. #14
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: LinkButton in Datalist to delete...verify first?

    The confirm JavaScript function returns a true or false for OK and cancel respectively. If the Cancel is pressed the returned false stops the PostBack.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    Re: LinkButton in Datalist to delete...verify first?

    Thanks. That makes sense. Thanks for ALL your help!
    Brenda

    If it weren't for you guys, where would I be?

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