ItemDataBound And LinkButton Attributes

C

Chris

Hello All,
I've got a problem that is probably very simple but it just isn't
working right for me.

I've been reading Scott Mitchell's article on DataGrids
(http://aspnet.4guysfromrolla.com/articles/090402-1.2.aspx) and
applying a lot of techniques to my app. I added delete button column
and I added an OnClick attribute on the ItemDataBound event that adds
the eventhandler to the button. The problem is that I get "Object
reference not set to an instance of an object." after the first row of
the datagrid is created.

Any ideas?

Chris

=============================================================================

Sub dgTierCodes_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles dgTierCodes.ItemDataBound
'Check for header and footer
If e.Item.ItemType <> ListItemType.Header And _
e.Item.ItemType <> ListItemType.Footer Then

'Now reference the LinkButtonControl that the Delete
'ButtonColumn is referenced to
Dim btnDeleteButton As LinkButton = _
e.Item.Cells(1).FindControl(0)

'Now create an OnClick EventHandler
btnDeleteButton.Attributes("onclick") = _
"javascript:return " & _
"confirm('Are you sure you want to delete Tier Code "
& _
DataBinder.Eval(e.Item.DataItem, "TierCode") & "?')"

End If
End Sub

=============================================================================
 
R

Rick Spiewak

I don't see any obvious errors, but a couple of suggestions:
1. Check explicitly for ListItemType.Item or ListItemType.AlternatingItem
instead of not header or footer. There is also a pager types, and other
types are legal as well
2. Use a template column so that you can use FindControl based on the ID of
the button instead of trying to locate it within the cells.
Set a breakpoint in your current code, step through it and see what item
type you have and what the layout of the cells is if you want to find out
what is wrong with the current code.

Chris said:
Hello All,
I've got a problem that is probably very simple but it just isn't
working right for me.

I've been reading Scott Mitchell's article on DataGrids
(http://aspnet.4guysfromrolla.com/articles/090402-1.2.aspx) and
applying a lot of techniques to my app. I added delete button column
and I added an OnClick attribute on the ItemDataBound event that adds
the eventhandler to the button. The problem is that I get "Object
reference not set to an instance of an object." after the first row of
the datagrid is created.

Any ideas?

Chris

============================================================================
=

Sub dgTierCodes_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles dgTierCodes.ItemDataBound
'Check for header and footer
If e.Item.ItemType <> ListItemType.Header And _
e.Item.ItemType <> ListItemType.Footer Then

'Now reference the LinkButtonControl that the Delete
'ButtonColumn is referenced to
Dim btnDeleteButton As LinkButton = _
e.Item.Cells(1).FindControl(0)

'Now create an OnClick EventHandler
btnDeleteButton.Attributes("onclick") = _
"javascript:return " & _
"confirm('Are you sure you want to delete Tier Code "
& _
DataBinder.Eval(e.Item.DataItem, "TierCode") & "?')"

End If
End Sub
============================================================================
=
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top