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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error When I added message box to my datalist delete button 1

Status
Not open for further replies.

ietprofessional

Programmer
Apr 1, 2004
267
US
Hi Everyone,

I have a datalist with an edit and delete button and both of them worked fine, until I wanted to put a message box confirmation for the delete button.

Now when I click the Edit button I'm getting this error on the lnkDelete.Attributes.Add... line:

Object reference not set to an instance of an object.

Code:
    Private Sub dlAcc_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlAcc.ItemDataBound
        If Not e.Item.ItemType = ListItemType.Header And Not e.Item.ItemType = ListItemType.Footer Then
            Dim lnkDelete As Button = CType(e.Item.FindControl("btnDelete"), Button)
            lnkDelete.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?');")
        End If
    End Sub

By the way, the delete button works fine.

Thanks!
 
I think you'll have to find the control from a certain cell. e.g.
Code:
Dim lnkDelete As Button = CType(e.Item.Cells(0).FindControl("btnDelete"), Button)

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

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
This is a datalist not a datagrid.
Can you give me anymore help on this one?
Thanks!
 
Make sure the button in the datalist has runat=server and ID=btnDelete attributes set.
 
Yes. The delete button has id = "btnDelete" and runat="server". The Edit button has id = "btnEdit" and runat = "server". I'm still unsure why this is happening.

Thanks!
 
Not sure why it happens. Never used a DataList before today. Just through together a quick test. Funny I did not get the exception until I hit Edit after coming back from a delete. This stopped the exception

If Not lnkDelete Is Nothing Then
lnkDelete.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?');")
End If

Hope it helps,
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top