e.Item.DataItem gives Null Reference after DataBind

K

Kel Good

Hello,

I am binding a custom IList object to a DataList that I am using for a web
menu. The items in the custom IList have properties that allow me to
dynamically define how my menu behaves. My binding syntax is standard:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

MenuItems.DataSource = GetMyCustomIList
MenuItems.DataBind()

End Sub

When the DataList binds, I access each list object in the ItemCreated event
of the DataList by casting the e.Item.DataItem to my custom list item type,
and then set the DataList Item properties from the values on my custom list
object:

Private Sub MenuItems_ItemCreated(ByVal sender As System.Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
MenuItems.ItemCreated

For Each controlItem As Control In e.Item.Controls

If TypeOf controlItem Is HyperLink Then

Dim menuItem As HyperLink = DirectCast(controlItem, HyperLink)
Dim menuInfo As MyCustomListItem = DirectCast(e.Item.DataItem,
myCustomListItem )

'Set the text elements for the menu item
menuItem.Text = menuInfo.Text
menuItem.NavigateUrl = menuInfo.NavigateUrl

'Set the font
menuItem.Font.Name = menuItem.FontName
menuItem.Font.Size = menuItem.FontSize

'Set the forecolor based on selection status
If e.Item.ItemIndex.ToString = Request.QueryString("id") Then
menuItem.ForeColor =
Color.FromName(MenuSettings.Property("SelectedColor").Value.String)
End If

End If

Next

End Sub


This all works fine when I bind the IList object to the DataList in the Page
Load method, but for some reason it isn't working when I do a postback to
the page. Whenever I do a PostBack, the e.Item.DataItem is Null. This is
true whether or not I've wrapped the DataBind code inside an If Not
IsPostback statement.

I've debugged to watch behavior. On a normal page load the DataBind command
results in the GetEnumerator method of my custom IList object being called.
In the PostBack scenario the GetEnumerator method does not seem to be being
called.

Any thoughts on why this would be?

Thanks.

Kel
 
S

Scott Mitchell [MVP]

Kel said:
I am binding a custom IList object to a DataList that I am using for a web
menu. The items in the custom IList have properties that allow me to
dynamically define how my menu behaves. My binding syntax is standard:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

[snip!]

This all works fine when I bind the IList object to the DataList in the Page
Load method, but for some reason it isn't working when I do a postback to
the page.

That's because the Web is stateless. On the first page load, you are
accessing the values from the database, so you can access the DataItem
property. The DataGrid's values are persisted in the ViewState (a
hidden form field sent down with the page), which is why the DataGrid
seems to remember its state across postbacks. However, on postback (as
you've discovered), DataItem is null because there's no DataSource being
bound to the DataGrid.

Can you not look up the data in the cells of the DataGrid, like:

e.Item.Cells(0).Text

Or something? If you absolutely need the database data on postback,
you've gotta go back and get it!


--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
K

Kel Good

Hi Scott,

Thanks for your reply. Actually, I was attempting to reload the datalist
on each page access/postback. It just wasn't working properly. Even
though I set the DataSource, the control didn't seem to retain this by
the time the item creation took place.

However, I have figured out my issue. I had read this several years ago
and it was floating in the back of my mind, but I couldn't remember the
details without experimentation.

I moved my control initialization code to the Page_Init event, rather
than the Page_Load event. This ensures the control gets initialized
before things that try to use it in the page. It's working as expected
now.

Thanks again for your thoughts.

Kel
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top