|
-
May 26th, 2005, 01:27 PM
#1
Thread Starter
Hyperactive Member
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!
Last edited by brendalisalowe; May 27th, 2005 at 11:19 AM.
Brenda
If it weren't for you guys, where would I be?
-
May 27th, 2005, 06:38 AM
#2
Frenzied Member
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!
-
May 27th, 2005, 10:05 AM
#3
Thread Starter
Hyperactive Member
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?
-
May 27th, 2005, 10:28 AM
#4
Frenzied Member
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!
-
May 27th, 2005, 10:40 AM
#5
Thread Starter
Hyperactive Member
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!
Brenda
If it weren't for you guys, where would I be?
-
May 27th, 2005, 10:49 AM
#6
Frenzied Member
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!
-
May 27th, 2005, 10:51 AM
#7
-
May 27th, 2005, 10:52 AM
#8
Frenzied Member
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!
-
May 27th, 2005, 10:53 AM
#9
Thread Starter
Hyperactive Member
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?
-
May 27th, 2005, 10:54 AM
#10
Frenzied Member
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!
-
May 27th, 2005, 10:56 AM
#11
Thread Starter
Hyperactive Member
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?
-
May 27th, 2005, 11:00 AM
#12
Frenzied Member
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!
-
May 27th, 2005, 11:04 AM
#13
Thread Starter
Hyperactive Member
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?
-
May 27th, 2005, 11:16 AM
#14
Frenzied Member
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!
-
May 27th, 2005, 11:18 AM
#15
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|