DataLists ItemCreated question

T

tshad

I am trying understand when DataLists ItemCreated is called.

I put function that attaches a popup to my delete button:

Sub DataList_ItemCreated(Sender As Object, e As DataListItemEventArgs)
trace.warn("Inside DataList_ItemCreated Sender = " &
sender.GetType().tostring())
trace.warn("Itemtype = " & e.Item.ItemType & " " &
e.Item.ItemType.ToString())
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.EditItem
trace.warn(" Setting up myDeleteButton")
Dim myDeleteButton As LinkButton
myDeleteButton = e.Item.FindControl("btnDel")
if not myDeleteButton is nothing
myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure
you want to delete this question?');")
end if
End Select
trace.warn("leaving DataList_ItemCreated")
End Sub

But it seems to be called all over the place. In my trace I have the
following (I won't put all of them there), including the traces from the
above routine.

*****************************************************************************
aspx.page Begin Init
aspx.page End Init
aspx.page Begin LoadViewState
Inside DataList_ItemCreated Sender = System.Web.UI.WebControls.DataList
Itemtype = 2 Item
Setting up myDeleteButton
leaving DataList_ItemCreated
Inside DataList_ItemCreated Sender = System.Web.UI.WebControls.DataList
Itemtype = 6 Separator
leaving DataList_ItemCreated
Inside DataList_ItemCreated Sender = System.Web.UI.WebControls.DataList
Itemtype = 3 AlternatingItem
Setting up myDeleteButton
leaving DataList_ItemCreated
....
aspx.page End LoadViewState
aspx.page Begin ProcessPostData
aspx.page End ProcessPostData
inside Page_load
aspx.page Begin ProcessPostData Second Try
aspx.page End ProcessPostData Second Try
aspx.page Begin Raise ChangedEvents
aspx.page End Raise ChangedEvents
aspx.page Begin Raise PostBackEvent
inside OnSelectIndex e.CommandName = Edit
inside DataListEdit
Inside DataList_ItemCreated Sender = System.Web.UI.WebControls.DataList
Itemtype = 2 Item
Setting up myDeleteButton
leaving DataList_ItemCreated
Inside DataList_ItemCreated Sender = System.Web.UI.WebControls.DataList
Itemtype = 6 Separator
leaving DataList_ItemCreated
Inside DataList_ItemCreated Sender = System.Web.UI.WebControls.DataList
Itemtype = 3 AlternatingItem
Setting up myDeleteButton
leaving DataList_ItemCreated
....
***********************************************************

The same ones that are called in LoadViewState are also called in Raise
PostBackEvent. How do I tell which one is the delete button?

I found the problem when I ran this and tried to push the button on the left
of the delete button. It gave me an error that the object wasn't there. If
I push the delete button it works fine. I assume this is because it isn't
created yet. I get the error in the Raise PostBackEvent and not in the
LoadViewState.

At the moment, the button is being set up in the ListItemType.Item,
ListItemType.AlternatingItem and ListItemType.EditItem. Not sure why, but
that was how it was done in the example code I got this from.

I would like to only attach it once. I am not sure why this was done in
this event. I would think I could set it in the Page_load event (but maybe
not).

Thanks,

Tom
 
S

Scott Allen

I am trying understand when DataLists ItemCreated is called.

I put function that attaches a popup to my delete button:

Tom:

Are you calling DataBind during the post back event? If so, I imagine
you are seeing ItemCreated fired once when the grid is restored from
ViewState, and once again when you data bind and recreate the grid.
Perhaps you'll want to attach to the ItemDataBound event, but I can't
tell for sure without digging into the code a little more.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top