JavaScript confirm

R

RN1

I have a DataList which displays the first & last name of users as
links (LinkButtons). When the links are clicked, 2 textboxes appear -
one with the first name & the other with the last name - for the users
to edit. At the same time, 3 LinkButtons - Cancel, Update & Delete
also appear. I want to add a JavaScript confirm dialog to the Delete
LinkButton. This is how I tried it in the OnItemDataBound event
function of the DataList:

----------------------------------
<script runat="server">
Sub BindItem(...........)
If (dlNetUsers.EditItemIndex <> -1) Then
If (ea.Item.ItemType = ListItemType.AlternatingItem Or
ea.Item.ItemType = ListItemType.Item) Then
Dim lnkDel As LinkButton
Dim lnkUID As LinkButton
lnkDel = CType(ea.Item.FindControl("lnkDelete"),
LinkButton)
lnkUID = CType(ea.Item.FindControl("lnkUserID"),
LinkButton)
lnkDel.Attributes.Add("OnClick", "if(!confirm('Delete
" & lnkUID.Text & "?')) return false;")
End If
End If
End Sub
</script>

<form runat="server">
<asp:DataList ID="dlUsers" OnItemDataBound="BindItem" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkUserID" CommandArgument='<%#
Container.DataItem("UserID") %>' CommandName="Edit" runat="server"><%#
Container.DataItem("FirstName") %>&nbsp;<%#
Container.DataItem("LastName") %></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFName" Text='<%# Container.DataItem("FirstName")
%>' runat="server"/>
<asp:TextBox ID="txtLName" Text='<%# Container.DataItem("LastName")
%>' runat="server"/>
<asp:LinkButton ID="lnkCancel" CommandName="Cancel" Text="Cancel"
runat="server"/>
<asp:LinkButton ID="lnkUpdate" CommandName="Update" Text="Update"
runat="server"/>
<asp:LinkButton ID="lnkDelete" CommandName="Delete" Text="Delete"
runat="server"/>
</EditItemTemplate>
</asp:DataList>
</form>
----------------------------------

The problem is when I click any of the names, the following error gets
generated:

Object reference not set to an instance of an object.

pointing to the line lnkDel.Attributes.Add........

What am I doing wrong here?

Thanks,

Ron
 
J

Joe Fawcett

I would check using view-source to see exactly what script is written to the
onclick attribute, probably a problem with quotes or similar.
You can also simplify the script to:
return !confirm('....')
 
H

Hans Kesting

Joe Fawcett wrote :
I would check using view-source to see exactly what script is written to the
onclick attribute, probably a problem with quotes or similar.
You can also simplify the script to:
return !confirm('....')

That will not work every time!
For instance linkbuttons add extra javascript to that onclick handler,
and your short version will ignore that (the linkbutton will not
submit) while the long version ("if(!confirm('...')) return false;", do
not forget the last ";") will still execute the extra code.

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top