How can i find a control in the footer template of a data repeater
-
10-07-2019 - |
Question
ASPX : Code
<asp:repeater id="repeater" runat="server">
<headerTemplate></headerTemplate>
<itemtemplate></itemtemplate>
<footerTemplate> <asp:literal id=findme runate=server> </footerTeplate>
</asp:repeater>
What i am looking for is source code to be able to find the control within the footer of a data repeater. Im familiar with the basic "FindControl" when i do a databind or look for control within the page itself, but how can i find the control within a footer template of a data repeater?
Is this even possible? and if so how can i please get some assistance,
thanks again to all!!!
[update]
i need to be able to do this after the databind
Solution
Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
If e.Item.ItemType = ListItemType.Footer Then
Dim Lit As Literal = CType(e.Item.FindControl("findme"), Literal)
End If
End Sub
OTHER TIPS
There are a number of ways that you can do it, the exact way depends on when you want to get access to the control.
If you want it during DataBind, simply do the following inside the item Databound.
if(e.Item.ItemType == ItemType.Footer)
{
Literal findMe = (Literal)e.Item.FindControl("findMe");
//Your code here
}
If you want to find it at another point in time, access the repeater's Item collection, then find the "Footer" item, and from that item, you can find the control.
Update
Based on your added note, you can do this by enumerating the item collection, below is an example with a repeater that has an id of myRepeater.
foreach (RepeaterItem item in myRepeater.Items)
{
if (item.ItemType == ListItemType.Footer)
{
Literal findMe = (Literal)item.FindControl("findMe");
//Do your stuff
}
}
I think you have to check the ListItemType in an ItemDataBound event handler. You can check for Header or Footer and then use the FindControl method to access the control.
Foreach (RepeaterItem item in myRepeater.Controls)
This will work better as Items collection doesn't contain header and footer
If you need to get the Footer after DataBind (which is what the OP appears to want) then you can use the following:
RepeaterItem item= (RepeaterItem)myRepeater.Controls[myRepeater.Controls.Count - 1];
if (item.ItemType == ListItemType.Footer) {
Literal findMe = (Literal)item.FindControl("findMe");
}