Accordion控件从数据库提取数据

//需先增加这个类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public delegate void InstantiateTemplateDelegate(Control container);
public class GenericTemplateImplementation : ITemplate
{
    private InstantiateTemplateDelegate m_instantiateTemplate;
    public void InstantiateIn(Control container)
    {
        m_instantiateTemplate(container);
    }
    public GenericTemplateImplementation(InstantiateTemplateDelegate instantiateTemplate)
    {
        m_instantiateTemplate = instantiateTemplate;
    }
}


<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </ajaxToolkit:ToolkitScriptManager>
                <ajaxToolkit:Accordion ID="Accordion1" runat="server">
                </ajaxToolkit:Accordion>
  
protected void Page_Load(object sender, EventArgs e)
    {
        LoadAccordion();
    }
    public SqlDataReader GetGroupColl(string sql)
    {
        return UserBase.USP_SELECT_OA_DynamicSQL(sql);
    }
    public SqlDataReader GetUserColl(string groupid)
    {
        string userSQL;
        if (groupid.Substring(0, 1) == "R")
            userSQL = "select a.id,a.username,a.loginname from crm_loginuser a,crm_roleloginuser b where a.id=b.loginUserID and b.roleid='" + groupid.Substring(1, 4) + "'";
        else
            userSQL = "select a.id,a.username,a.loginname from crm_loginuser a,crm_org b where a.orgid=b.orgid and b.orgid='" + groupid.Substring(1, 4) + "'";
        return UserBase.USP_SELECT_OA_DynamicSQL(userSQL);
    }
    protected void LoadAccordion()
    {
        string CurrentGroupSQL = "";
        SqlDataReader drmain = UserBase.USP_SELECT_OA_UserQuerySort();

        Accordion1.SelectedIndex = -1;
        int i = 0;
        while (drmain.Read())
        {
            i++;
            AjaxControlToolkit.AccordionPane ap = new AjaxControlToolkit.AccordionPane();
            ap.ID = "AP_" + i.ToString();
            string hlabel = drmain["QueryName"].ToString();//这里取大的分类
            CurrentGroupSQL = drmain["GroupSQL"].ToString();
            ITemplate ITheader = new GenericTemplateImplementation(
                delegate(Control container)
                {
                    Label headerLabel = new Label();
                    headerLabel.ID = "HL_" + i.ToString();
                    headerLabel.Text = "<table border='0' cellpadding='0' cellspacing='0' style='100%;'><tr><td class='UserMenu' valign='middle'>&nbsp;" + hlabel + "</td></tr></table>";
                    // headerLabel.CssClass = "UserMenu";
                    container.Controls.Add(headerLabel);
                }
            );
            //取组,角色和组织
            SqlDataReader drGroup = GetGroupColl(CurrentGroupSQL);

            ITemplate ITcontent = new GenericTemplateImplementation(
                delegate(Control container)
                {
                    AjaxControlToolkit.Accordion ac = new AjaxControlToolkit.Accordion();
                    ac.ID = "ACCC_" + i.ToString();
                    ac.HeaderCssClass = "accordionHeader";
                    ac.HeaderSelectedCssClass = "accordionHeaderSelected";
                    ac.ContentCssClass = "accordionContent";
                    ac.SelectedIndex = -1;

                    int x = 0;
                    while (drGroup.Read())
                    {
                        x++;
                        AjaxControlToolkit.AccordionPane apx = new AjaxControlToolkit.AccordionPane();
                        apx.ID = "APx_" + x.ToString();
                        string hxlabel = drGroup["ClassName"].ToString();
                        SqlDataReader drEmployee = GetUserColl(drGroup["id"].ToString());//得到用户
                        ITemplate ITxheader = new GenericTemplateImplementation(
                            delegate(Control container2)
                            {
                                Label xheaderLabel = new Label();
                                xheaderLabel.ID = "HLx_" + x.ToString();
                                xheaderLabel.Text = "<div class='UserHeight'><img  src='images/userarrow1.png' />&nbsp;" + hxlabel + "</div>";
                                xheaderLabel.CssClass = "UserArraw";
                                container2.Controls.Add(xheaderLabel);
                            }
                        );


                        string xcLabel = "";
                        string sEmployee = "";
                        string sEmployeeID = "";
                        while (drEmployee.Read())
                        {
                            sEmployee = drEmployee["username"].ToString();
                            sEmployeeID = drEmployee["id"].ToString();
                            xcLabel = xcLabel + "&nbsp;&nbsp;<img  src='images/addrss.gif' /><a href='' TagName=" + sEmployeeID.Trim() + "," + sEmployee.Trim() + "   onclick='addFile(this);return false;'>" + sEmployee + "</a><br/>";
                        }
                        drEmployee.Close();
                        drEmployee.Dispose();
                        ITemplate ITxcontent = new GenericTemplateImplementation(
                            delegate(Control container3)
                            {
                                Label xcontentLabel = new Label();
                                xcontentLabel.ID = "CLx_" + x.ToString();
                                xcontentLabel.Text = xcLabel;
                                container3.Controls.Add(xcontentLabel);
                            }
                        );
                        apx.Header = ITxheader;
                        apx.Content = ITxcontent;
                        ac.Panes.Add(apx);
                    }
                    ac.SelectedIndex = -1;
                    container.Controls.Add(ac);
                    drGroup.Close();
                    drGroup.Dispose();
                }
            );
            ap.Header = ITheader;
            ap.Content = ITcontent;

            Accordion1.Panes.Add(ap);
        }

        drmain.Close();
        drmain.Dispose();

    }

原文地址:https://www.cnblogs.com/hhq80/p/1398192.html