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
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