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:
Sub DataList_DeleteCommand(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
Dim conn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings().Item("strConn"))
Dim acctno As String = DataList1.DataKeys(e.Item.ItemIndex)
Dim sql As String = "DELETE FROM CustomerRecords " & _
"WHERE ConsumerAccountNumber = '" & acctno & "'"
Dim cmd As New SqlCommand(sql, conn)
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message)
Finally
If Not conn Is Nothing Then
conn.Dispose()
End If
End Try
DataList1.EditItemIndex = -1
DataBinder()
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!
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
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>
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
Re: LinkButton in Datalist to delete...verify first?
So I don't put the above code inside this:
VB Code:
Sub DataList_DeleteCommand(ByVal sender As Object, ByVal e As DataListCommandEventArgs)
End Sub
And I've tried this but it doesn't work. Is it set up right?
VB Code:
Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As DataListCommandEventArgs) 'Is this part right?
If (e.Item.ItemType = ListItemType.Item And e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim deleteButton As LinkButton = e.Item.FindControl("lnkDelete")
deleteButton.Attributes("OnClick") = "javascript:return confirm('Are you sure you want to delete this entry?')"
End If
End Sub
Also, I am not hot in C# so can anyone else help me out with that? Thanks!
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
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
Re: LinkButton in Datalist to delete...verify first?
Did you add the OnItemDataBound="DataList1_ItemDataBound" to the DataList tag?
DJ
Re: LinkButton in Datalist to delete...verify first?
But does || mean AND or OR?
Re: LinkButton in Datalist to delete...verify first?
Actually it should be an Or not an And in the if statement.
DJ
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?
Re: LinkButton in Datalist to delete...verify first?
Ah yes use DataListItemEventArgs instead of DataListCommandEventArgs.
DJ
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?
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
Re: LinkButton in Datalist to delete...verify first?
Thanks. That makes sense. Thanks for ALL your help!