How to find controls in a repeater header or footer

前端 未结 8 2125
离开以前
离开以前 2020-11-28 02:19

I was wondering how one would find the controls in the HeaderTemplate or FooterTemplate of an Asp.Net Repeater control.

I can access them on the ItemDataBound event,

相关标签:
8条回答
  • 2020-11-28 02:58

    As noted in the comments, this only works AFTER you've DataBound your repeater.

    To find a control in the header:

    lblControl = repeater1.Controls[0].Controls[0].FindControl("lblControl");
    

    To find a control in the footer:

    lblControl = repeater1.Controls[repeater1.Controls.Count - 1].Controls[0].FindControl("lblControl");
    

    With extension methods

    public static class RepeaterExtensionMethods
    {
        public static Control FindControlInHeader(this Repeater repeater, string controlName)
        {
            return repeater.Controls[0].Controls[0].FindControl(controlName);
        }
    
        public static Control FindControlInFooter(this Repeater repeater, string controlName)
        {
            return repeater.Controls[repeater.Controls.Count - 1].Controls[0].FindControl(controlName);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 03:01

    Find control into Repeater (Header, Item, Footer)

    public static class FindControlInRepeater
    {
        public static Control FindControl(this Repeater repeater, string controlName)
        {
            for (int i = 0; i < repeater.Controls.Count; i++)
                if (repeater.Controls[i].Controls[0].FindControl(controlName) != null)
                    return repeater.Controls[i].Controls[0].FindControl(controlName);
            return null;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 03:01
    private T GetHeaderControl<T>(Repeater rp, string id) where T : Control
    {
        T returnValue = null;
        if (rp != null && !String.IsNullOrWhiteSpace(id))
        {
            returnValue = rp.Controls.Cast<RepeaterItem>().Where(i => i.ItemType == ListItemType.Header).Select(h => h.FindControl(id) as T).Where(c => c != null).FirstOrDefault();
        }
        return returnValue;
    }
    

    Finds and casts the control. (Based on Piyey's VB answer)

    0 讨论(0)
  • 2020-11-28 03:05

    Better solution

    You can check item type in ItemCreated event:

    protected void rptSummary_ItemCreated(Object sender, RepeaterItemEventArgs e) {
        if (e.Item.ItemType == ListItemType.Footer) {
            e.Item.FindControl(ctrl);
        }
        if (e.Item.ItemType == ListItemType.Header) {
            e.Item.FindControl(ctrl);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 03:15

    This is in VB.NET, just translate to C# if you need it:

    <Extension()>
    Public Function FindControlInRepeaterHeader(Of T As Control)(obj As Repeater, ControlName As String) As T
        Dim ctrl As T = TryCast((From item As RepeaterItem In obj.Controls
                       Where item.ItemType = ListItemType.Header).SingleOrDefault.FindControl(ControlName),T)
        Return ctrl
    End Function
    

    And use it easy:

    Dim txt as string = rptrComentarios.FindControlInRepeaterHeader(Of Label)("lblVerTodosComentarios").Text
    

    Try to make it work with footer, and items controls too =)

    0 讨论(0)
  • 2020-11-28 03:15

    For ItemDataBound

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)//header
        {
                Control ctrl = e.Item.FindControl("ctrlID");
        }
        else if (e.Item.ItemType == ListItemType.Footer)//footer
        {
                Control ctrl = e.Item.FindControl("ctrlID");
        }
    }
    
    0 讨论(0)
提交回复
热议问题