首页 > 代码库 > 学习笔记找到多个具有相同ID“_header”的控件,FindControl要求控件具有唯一的ID.

学习笔记找到多个具有相同ID“_header”的控件,FindControl要求控件具有唯一的ID.

private void DisplayHotBooks()
    {
        //获取所有的书分类信息
        IList<Category> list = CategoryManager.GetAllCategories();
        int i = 0;
        foreach (Category item in list)
        {
            AccordionPane ap = new AccordionPane();
            Label lbMenu = new Label();
            lbMenu.Text = item.Name;
            ap.HeaderContainer.Controls.Add(lbMenu);
            Accordion1.Panes.Add(ap);
        }
    }

从异常"_header"可以猜测到与pane的header有关;于是手动添加不重复id

    private void DisplayHotBooks()
    {
        //获取所有的书分类信息
        IList<Category> list = CategoryManager.GetAllCategories();
        int i = 0;
        foreach (Category item in list)
        {
            AccordionPane ap = new AccordionPane();
            Label lbMenu = new Label();
            lbMenu.Text = item.Name;
            ap.HeaderContainer.Controls.Add(lbMenu);
            ap.HeaderContainer.ID = "_header" + item.Id;
            Accordion1.Panes.Add(ap);
        }
    }

接着抛出异常 找到多个具有相同 ID“_content”的控件。FindControl 要求控件具有唯一的 ID。呵呵,差点忘了pane除了header还有content,于是再手动添加content id;最终如下:

    private void DisplayHotBooks()
    {
        //获取所有的书分类信息
        IList<Category> list = CategoryManager.GetAllCategories();
        int i = 0;
        foreach (Category item in list)
        {
            AccordionPane ap = new AccordionPane();
            Label lbMenu = new Label();
            lbMenu.Text = item.Name;
            ap.HeaderContainer.Controls.Add(lbMenu);
            ap.HeaderContainer.ID = "_header" + item.Id;
            ap.ContentContainer.ID = "_content" + item.Id;
            Accordion1.Panes.Add(ap);
        }
    }