代码之家  ›  专栏  ›  技术社区  ›  mbillard

如何在中继器页眉或页脚中查找控件

  •  122
  • mbillard  · 技术社区  · 17 年前

    我可以在ItemDataBound事件中访问它们,但我想知道如何在之后获取它们(例如检索页眉/页脚中的输入值)。

    注意:我在找到答案后在这里发布了这个问题,只是为了记住它(也许其他人会觉得这很有用)。

    8 回复  |  直到 17 年前
        1
  •  175
  •   Shog9    12 年前

    正如评论中所指出的,这只有在您对中继器进行数据绑定后才有效。

    要在中查找控件 头球 :

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

    要在中查找控件 页脚 :

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

    使用扩展方法

    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);
        }
    }
    
        2
  •  54
  •   David Rogers    9 年前

    更好的解决方案

    您可以在ItemCreated事件中检查项目类型:

    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);
        }
    }
    
        3
  •  5
  •   pascal    16 年前

    您可以在ItemCreated事件上引用控件,然后稍后使用它。

        4
  •  4
  •   Chaki_Black    15 年前

    在中继器中查找控件(页眉、项目、页脚)

    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;
        }
    }
    
        5
  •  2
  •   Piyey    14 年前

    这是在VB.NET中,如果你需要的话,可以翻译成C#:

    <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
    

    而且使用起来很简单:

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

    尝试使其与页脚和项目控件一起工作=)

        6
  •  2
  •   Sebastien H.    12 年前

    最好和干净的方法是在Item_Created事件中:

     protected void rptSummary_ItemCreated(Object sender, RepeaterItemEventArgs e)
            {
                switch (e.Item.ItemType)
                {
                    case ListItemType.AlternatingItem:
                        break;
                    case ListItemType.EditItem:
                        break;
                    case ListItemType.Footer:
                        e.Item.FindControl(ctrl);
                        break;
                    case ListItemType.Header:
                        break;
                    case ListItemType.Item:
                        break;
                    case ListItemType.Pager:
                        break;
                    case ListItemType.SelectedItem:
                        break;
                    case ListItemType.Separator:
                        break;
                    default:
                        break;
                }
        }
    
        7
  •  0
  •   glboothby    8 年前
    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;
    }
    

    查找并强制转换控件。(基于Piyey的VB答案)

        8
  •  0
  •   Claudinei Ferreira    7 年前

    对于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");
        }
    }
    
    推荐文章