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.EventArg s) Handles MyBase.Load
MenuItems.DataS ource = GetMyCustomILis t
MenuItems.DataB ind()
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_ItemC reated(ByVal sender As System.Object, ByVal e As
System.Web.UI.W ebControls.Data ListItemEventAr gs) Handles
MenuItems.ItemC reated
For Each controlItem As Control In e.Item.Controls
If TypeOf controlItem Is HyperLink Then
Dim menuItem As HyperLink = DirectCast(cont rolItem, HyperLink)
Dim menuInfo As MyCustomListIte m = DirectCast(e.It em.DataItem,
myCustomListIte m )
'Set the text elements for the menu item
menuItem.Text = menuInfo.Text
menuItem.Naviga teUrl = menuInfo.Naviga teUrl
'Set the font
menuItem.Font.N ame = menuItem.FontNa me
menuItem.Font.S ize = menuItem.FontSi ze
'Set the forecolor based on selection status
If e.Item.ItemInde x.ToString = Request.QuerySt ring("id") Then
menuItem.ForeCo lor =
Color.FromName( MenuSettings.Pr operty("Selecte dColor").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 2 3939
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.EventArg s) 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******@4guysf romrolla.com http://www.4GuysFromRolla.com http://www.ASPFAQs.com http://www.ASPMessageboard.com
* When you think ASP, think 4GuysFromRolla. com!
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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 that the
datasource is being populated properly, however the itemdatabound
event is not being fired. Here's some code:
<asp:datalist...
|
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 the first item in the
dropdown is always displayed.
Template Code:
<asp:TemplateColumn HeaderText="DropDown">
<ItemTemplate>
<asp:Label...
|
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 table. Easy stuff so far. My
table contains typical Address and Phone numbers data. What I'd like to do
is check to see if "Address2" (field) is empty...
|
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();
//(in OnSelectedIndexChanged)
MyTableRow row = myGrid.SelectedItem.DataItem as MyTableRow;
myLabel.Text = "You selected: " + row.Name;
|
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 page I can see all my data fine.
Now I want to add a textbox for each row and pre-populate that box with
values from my DataTable. So I created a...
| |
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:
------------------------------------------------------------------
<ASP:DataList id="MyDataList" RepeatColumns="3" runat="server">
<ItemTemplate>
<table...
|
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 checkbox in the datagrid, if it is checked, it updates the
corresponding rows prices.
That works, no problem.
What I need to do is that when a user...
|
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"
OnItemDataBound="Bind">
<headertemplate>
<ul>
</headertemplate>
|
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). But... and this is the crappy
part, the nested repeater's events are fired twice. How do I know this? I
setup global private counter variables...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |