473,474 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

e.Item.DataItem gives Null Reference after DataBind

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("SelectedColo r").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
Nov 18 '05 #1
2 3928
Kel Good wrote:
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
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
Nov 18 '05 #2

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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Barry | last post by:
Hi, I have a Datalist inside the ItemTemplate of another DataList. When I drilldown, I'm not getting any data in either the itemtemplate or headertemplate of the second DataList. I've confirmed...
7
by: localhost | last post by:
A DataGrid with shows a label in one of the columns when in view mode. When in edit mode, I want to show a dropdown, and have the default selection set to what the textbox used to be. Right now...
2
by: D. Shane Fowlkes | last post by:
I'm using ASP.NET (VB) and I'm pretty new to .NET and using Templates. I have a SqlDataReader which is bound to a Repeater. The Repeater contains a table and for each record, it'll write a...
2
by: Joel Lyons | last post by:
The help pages make it look like the DataGridItem.DataItem property would return a row from the bound table. Like so: //(during setup) myGrid.DataSource = myDataset.MyTable; myGrid.DataBind();...
3
by: MattB | last post by:
I swear I've made this work no problem before and I'm not sure what's gone wrong. I have a form with a datagrid that I'm binding to a DatTable I generate on the fly. If I just bind and load the...
1
by: lydia sista via DotNetMonster.com | last post by:
hi! help... How do you show/hide item's data? Like sale item on product catalogue. I want to show the sale image when the status of product is on sale. in my catalogue I use datalist:...
2
by: Luis Esteban Valencia Muñoz | last post by:
I have a datagrid that displays editable text fields (2 different price fields) and a checkbox in every row. It has a "SaveChanges" button at the bottom, which, when pressed, looks at every...
2
by: ibiza | last post by:
Hi all, I have a simple scenario where I simply bien a repeater to a datasource. Here's my repeater definition : <asp:Repeater id="rTitles" runat="server" EnableViewState="false"...
4
by: =?Utf-8?B?SmFtZXMgR2V1cnRz?= | last post by:
On my page, I have one repeater that contains a literal control and a nested repeater. The nested repeater contains a literal control. Both repeaters are databound with only one object (string). ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.