Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't find Footer in DataList

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I am trying to make an object visible that is in the footer of my DataList.

I don't want to use the ItemDataBound event to do this but want to do something like:

Code:
            foreach (DataListItem dli in DataList1.Items)
            {
                switch (dli.ItemType)
                {
                    case ListItemType.Footer:
                        break;
                    case ListItemType.Item:
                        break;
                    case ListItemType.AlternatingItem:
                        break;
                    case ListItemType.Header:
                        break;
                }
            }

The problem is that it only shows me the Item and Alternating Item. It does not get to a Header of Footer item. It does find them during the ItemDataBound.

Why doesn't going through the Items of the DataList find them?

Thanks,

Tom
 
I know.

I at times want to go through the whole datalist to make some changes in the PreRender event.

But Looping through the DataList object doesn't seem to show that.

Thanks,

Tom
 
I still don't understand what you can do in the pre_render vs the ItemDataBound. There is nothing. That is why that event exists and where your formatting etc can and should be done.
 
Here is a reason I might need to go through the list and not from the ItemDataBound event.

I have a LinkButton on all the Items of the DataList and if someone presses the LinkButton, I want to hide the panel that is in the Footer.

I need to go through the list to find the Footer and then do a FindControl from there to find the panel that is in the footer.

There is no ItemDataBound event taking place here.

Thanks,

Tom
 
If a linkbutton is pressed, a postback will occur. Most likely your code will rebind the the listview and that event will fire.
If you simply want to hide a panel, then use javascript or better yet JQuery to hide the panel, and this way you avoid the postback.
 
So I don't understand.

Are you saying there is no way to get to the Footer and Header except by the event?

And that you can only get to the Item and AlternateItem looping through the DataList items?

Thanks,

Tom
 
Found it.

You can get the Header and Footer directly using the Controls list. It is apparently not part of the Items list.

You don't need to (but can) go through the Controls list in a foreach statement:
Code:
            foreach ( Control item in DataList1.Controls )
             {
                if(item.FindControl("AddQuestion") != null)
                    i = i;
            }

But there doesn't seem to be a way to tell what type of control you have. A GetType() will just tell you that you have a DataListItem. So this is not the best way to do it as you won't know you have a footer until the FindControl is null. The problem here is if you have 2 or more controls with the same name - as you normall do in a Datalist or DataGrid.

The better way to do it would be to call it directly, the first control will always be a Header (if there is one) and the last control will always be a footer (if there is one).

So, for the header:

DataList1.Controls[0].FindControl("AddQuestion")

and for the footer:

DataList1.Controls[DataList1.Controls.Count - 1].FindControl("AddQuestion")

It would be nice if there were some way to tell that you have a footer but there you go.


Thanks,

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top