Jeffrey Palermo (.com)

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
DataList control tree and accessing the header and footer - level 300

Recently I converted a DataGrid to a DataList because I decided that I didn't want my two pieces of data in different table cells.  After converting it to a DataList, I ran into an issue setting the Header text and Footer text programmatically.  I had my HeaderTemplate with a asp:Literal in there.  From my code, I expected to be able to do DataList.Header.FindControl(), but Header isn't a property, and the HeaderTemplate property is just the interface definition. 

1.  The control doesn't even exist until the DataList.DataBind() method is run.

2.  Looking at the control tree in the trace, I find that it renders as DataList:_ctl0:myControl for the Header and DataList:_ctl{Count-1}:myControl for the Footer.

3.  To make it work, I have to use DataList.Controls[0].FindControl(”myControl”) for the header and DataList.Controls[Count - 1].FindControl(”myControl”).

So what is the reliable way to get the Header and Footer.  Especially for the footer, I have to get the last control in the Controls collection.  The header and footer should be separate from the rest of the bound list, but that, apparantly, is the case.

I would prefer a more elegant solution and one that is more like the rest of the classes in the .Net Framework.  For instance, the Header and Footer should be able to accept and ID property for control naming.  Better yet, they should be properties of the DataList, but since they don't exist until runtime, we should at least be able to FindControl(”HeaderID”) at runtime, so the ID property would solve that.

Another quirk in the ASP.NET controls, but there is a workaround.


Posted 06-02-2004 12:32 PM by Jeffrey Palermo

[Advertisement]

Comments

Peter van Ooijen wrote re: DataList control tree and accessing the header and footer - level 300
on 06-03-2004 2:44 AM
Hi,
why not create you own datalist. Inherit from datalist and add your own header property. No big deal : http://www.dotnetjunkies.com/Tutorial/6614EF8F-2ADB-4AEB-B2B7-D96278B53D74.dcik

Templates are not very nice to work with from code. When some coding on a template is needed I drop an usercontrol on the template and do my coding in there.
Jeffrey Palermo wrote re: DataList control tree and accessing the header and footer - level 300
on 06-03-2004 6:49 AM
That's a good point, and I could easily do that, and I may. Right now I have a UserControl that is only the DataList, so I have added a HeaderText and FooterText property to the UserControl, and I handle it that way. I agree with you, though. What I was perturbed about was that this functionality isn't included in the DataList object.
Peter van Ooijen wrote re: DataList control tree and accessing the header and footer - level 300
on 06-04-2004 3:32 AM
My main problem with the datalist are the templates. Flexible but unfriendly to program. They have been overhauled in Whidbey. Looks like they support events now. But no class members yet (?)
tim wrote re: DataList control tree and accessing the header and footer - level 300
on 09-07-2004 1:09 PM
I put I label on the header and next i databind it to a Context.Items("headercaption")

before the databind takes place i set Context.Items("headercaption") to what i like

works perfectly.
eoghan wrote Question
on 11-17-2004 11:35 PM
Trying to work around this exact problem:

Where do you get the 'Count' in DataList.Controls[Count - 1] in order to access the footer?

(pity we couldn't do it with elegant Python indexing:
Datalist.Controls[-1] mirroring Datalist.Controls[0])
Leon wrote re: DataList control tree and accessing the header and footer - level 300
on 09-07-2005 10:28 AM
There is a quick and simple way to interact with the Header and Footer at Runtime. Simple handle the ItemDataBound event for your datalist. eg: To access your literal control that is in your header you'd add this to that method:

if(e.Item.ItemType == ListItemType.Header)
{
Literal lit = (Literal)e.Item.FindControl("litHeader");
lit.Text = "Hello World";
}

That will do the trick, you can also look for the Footer, Item, AltItems...whatever you like! :)
Steve wrote re: DataList control tree and accessing the header and footer - level 300
on 02-02-2007 3:47 PM

Leon, thanks for the concise and accurate answer.  Exactly what I needed.

Steve wrote re: DataList control tree and accessing the header and footer - level 300
on 02-02-2007 4:03 PM

For those new to .NET here are a few more details to help you implement Leon's advice:

To handle the ItemDataBound event:  Go to the aspx or ascx source page where the DataList control is declared.  Inside the asp DataList tag <asp:DataList ... yadda> add the attribute OnItemDataBound="your_method_name_here".  I prefer the method name DataList1_OnItemDataBound however.

To code the handler:  Add a method in your codebehind or inline script with the following signature (C#)

 protected void your_method_name_here(object sender, DataListItemEventArgs e)

Place Leon's (or preferrably your) code in the aforementioned handler method and enjoy the results.