Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript confirmation on delete 1

Status
Not open for further replies.

davikokar

Technical User
Joined
May 13, 2004
Messages
523
Location
IT
This question has been posted many times. But still, with all the material available online I don't understand why I cannot make the code below working. Maybe it's because I'm using datalist instead of a datagrid.
Do you see something wrong?
thanks

Code:
Private Sub Item_Created_mes(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
    ' process data rows only (skip the header, footer etc.)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        ' get a reference to the LinkButton of this row,
        '  and add the javascript confirmation
        Dim lbtdeleteMes As LinkButton = CType(e.Item.FindControl("lbtdelete"), LinkButton)
        lbtdeleteMes.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?');")
        
    End If
End Sub
 
The following worked fine for me:
Code:
    Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemCreated
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim lbtdeleteMes As LinkButton = CType(e.Item.FindControl("lbtdelete"), LinkButton)
            lbtdeleteMes.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?');")
        End If
    End Sub

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
thanks camsm8 for the reply. I tried to use your code (very similar to mine) but I still get the same error "object reference not set to an instance of the object" on the line:

lbtdeleteMes.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?');")

can it be something with the datalist code?

Code:
<asp:Label id="lblMessage" runat="server" />
<ASP:DataList id="DataList1" runat="server"
RepeatLayout="table"
DataKeyField="mes_ID"
cssclass="cont_blog"
OnEditCommand="Datalist1_EditCommand"
OnCancelCommand="DataList1_CancelCommand"
OnUpdateCommand="DataList1_UpdateCommand"
OnDeleteCommand="DataList1_DeleteCommand"
OnItemCreated="Datalist1_ItemCreated">

....

<EditItemTemplate>
<tr>
   <td><asp:TextBox Text='<%# Container.DataItem("mes_title")%>' runat="server" ID="edit_title" CssClass="input_title" /></td>
</tr>
<tr>
   <td><a href="myself_blog_det.aspx?id=<%#Container.DataItem("mes_id")%>">VIEW COMMENTS</a> | <a href="myself_blog_addcom.aspx?id=<%#Container.DataItem("mes_id")%>">WRITE COMMENT</a> | <asp:LinkButton id="lbtCancel" runat="server" text="CANCEL" CommandName="Cancel" /> | <asp:LinkButton id="lbtUpdate" runat="server" text="SAVE" CommandName="Update" CommandArgument='<%# DataBinder.eval(Container.DataItem, "mes_id")%>' /> | <asp:LinkButton id="lbtdelete" runat="server" text="DELETE" CommandName="Delete" CommandArgument='<%# DataBinder.eval(Container.DataItem, "mes_id")%>' /></td>
</tr>
</EditItemTemplate>

</ASP:datalist>
 
Err yes...you don't have a link button named lbtdeleteMes!

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
yes, the linkbutton I would like to trigger the javascript is the lbtdelete... but if I refer only to this button in the code behind I'll get anyway the error. All the examples I've seen were referring to two different buttons...
CType(e.Item.FindControl("lbtdelete"), LinkButton)
and
lbtdeleteMes.Attributes.Add
so lbtdelete and lbtdeleteMes.

i don't understand.... why two different buttons if the button who should do the job is one? I need enlightment.
 
Try this example:
Code:
			<asp:DataList id="DataList1"runat="server">
				<ItemTemplate>
					<asp:LinkButton id="lbtdelete" runat="server">LinkButton</asp:LinkButton>
				</ItemTemplate>
			</asp:DataList>
and:
Code:
Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemCreated
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim lbtdeleteMes As LinkButton = CType(e.Item.FindControl("lbtdelete"), LinkButton)
            lbtdeleteMes.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?');")
        End If
    End Sub
What you will see is that on the page there is a button named lbtdelete (see top code). When we come to the ItemCreated event we set a button named lbtdeleteMes to be the button from the form named lbtdelete (we have to use FindControl as the link button is repeated for each row).

This is a simple as it can be broken down so you should be able to understand and use this.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
ca8msm,
your example is basically the first version I was using...

the only difference I see in your example is that your lbtdelete button is in the itemtemplate, while in my case it is in the edititemtemplate...
 
I don't know what you are asking then. I've given you an example that works so can't you just use this and modify it as neccessary?

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
well, it's my original question: it should work but it doesn't... what is wrong with?

Here is the code, that is basically the same as your example. Or maybe is not?

datalist:
Code:
<ASP:DataList id="DataList1" runat="server"
DataKeyField="mes_ID"
OnEditCommand="Datalist1_EditCommand"
OnCancelCommand="DataList1_CancelCommand"
OnUpdateCommand="DataList1_UpdateCommand"
OnDeleteCommand="DataList1_DeleteCommand"
OnItemCreated="Datalist1_ItemCreated">

<EditItemTemplate>
<tr>
   <td><asp:TextBox Text='<%# Container.DataItem("mes_title")%>' runat="server" ID="edit_title" /></td>
</tr>
<tr>
   <td><asp:LinkButton id="lbtdelete" runat="server" text="DELETE" CommandName="Delete" CssClass="blog_a_options" CommandArgument='<%# DataBinder.eval(Container.DataItem, "mes_id")%>' /></td>
</tr>
</EditItemTemplate>

</ASP:datalist>

code behind:
Code:
Private Sub Datalist1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemCreated
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim lbtdeleteMes As LinkButton = CType(e.Item.FindControl("lbtdelete"), LinkButton)
            lbtdeleteMes.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?');")
        End If
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top