将ListView绑定到动态Accordion

时间:2012-07-23 21:09:29

标签: c# asp.net listview accordion

page_load中,我使用来自SpGetCompanies的数据动态创建标头,这是正常的。然后我尝试为每个动态创建的手风琴窗格添加ListView,其中包含数据源SpSearchPublicationsTop1

protected void Page_Load(object sender, EventArgs e)
    {    
       if (!this.IsPostBack)
        {
            SQLConnection dataAccessor = new SQLConnection();
            dtCompanies = dataAccessor.SpGetCompanies();

            if (dtCompanies.Rows.Count > 0)
            {
                AccordionPane pn;

                Label lblContent;
                int j = 0;
                for (int i = 0; i < dtCompanies.Rows.Count; i++)
                {   
                    lblHeader = new Label();
                    lblHeader.Text = dtCompanies.Rows[i][1].ToString();

                    lblContent = new Label();                       
                    lblContent.Text = "Hallo";

                    pn = new AccordionPane();
                    pn.ID = "Pane" + j;
                    pn.HeaderContainer.Controls.Add(lblHeader);

                    ListView view = new ListView();

                    view.LayoutTemplate = new LayoutTemplate();
                    view.ItemTemplate = new ItemTemplate();
                    //view = (ListView)FindControl("lv_result");

                    view.DataSource = SQLConnection.SpSearchPublicationsTop1(lblHeader.Text);
                    view.DataBind();

                    #region commented stuff
                    //dt2 = SQLConnection.SpSearchPublicationsTop1(lblHeader.Text);

                    //if (dt2.Rows.Count > 0)
                    //{
                    //    //view = (ListView)FindControl("lv_result");

                    //    //column1.Text = dt2.Rows[0][0].ToString();                       
                    //    //view.Columns.Add(column1);
                    //    //Label lblAcronym = new Label();
                    //    //lblAcronym.Text = dt2.Rows[0][0].ToString();

                    //    //view.DataSource = lblAcronym;
                    //    view.DataSource = dt2;
                    //    view.DataBind();

                    //}

                    //listView1.Columns.Add(column1);

                    //foreach (DataRow dr in dt2.Rows)
                    //{
                    //    ListViewItem lvi = new ListViewItem(dr[0].ToString()); //1.st column in datatable, instead of 0 you can write column`s name like: ["CustomerID"]
                    //    lvi.SubItems.Add(dr[1].ToString()); //2nd column from datatable
                    //    view.Items.Add(lvi);
                    //}

                    //this.Controls.Add(view);

                    //ListView lv_result = new ListView();
                    ////lv_result.ItemTemplate = new ItemTemplate();
                    //lv_result.ID = "lvpane" + i;
                    //lv_result.Visible = true;

                    //dt2 = SQLConnection.SpSearchPublicationsTop1(lblHeader.Text);

                    //if (dt2.Rows.Count > 0)
                    //{
                    //    lv_result.DataSource = dt2;
                    //    lv_result.DataBind();
                    //}

                    //var level2 = (ListView)FindControl("lv_result");
                    //level2.ItemTemplate = view.ItemTemplate;

                    ////level2.DataSource = dt2(e.AccordionItem.DataItemIndex);
                    ////level2.DataBind();

                    //dt2 = SQLConnection.SpSearchPublicationsTop1(lblHeader.Text);

                    //if (dt2.Rows.Count > 0)
                    //{
                    //    level2.DataSource = dt2;
                    //    level2.DataBind();
                    //}
                    #endregion


                    pn.ContentContainer.Controls.Add(view);
                    //pn.ContentContainer.Controls.Add(lblContent);
                    Accordion1.Panes.Add(pn);
                    ++j;
                }
            }
        }

    }

 private class LayoutTemplate : ITemplate
    {

        public void InstantiateIn(Control container)
        {
            var ol = new HtmlGenericControl("ol");
            var li = new HtmlGenericControl("li") { ID = "itemPlaceholder" };
            ol.Controls.Add(li);

            container.Controls.Add(ol);
        }
    }

    private class ItemTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            var li = new HtmlGenericControl("li");

            li.DataBinding += DataBinding;
            container.Controls.Add(li);
        }

        public void DataBinding(object sender, EventArgs e)
        {
            var container = (HtmlGenericControl)sender;
            var dataItem = ((ListViewDataItem)container.NamingContainer).DataItem;

            container.Controls.Add(new Literal() { Text = dataItem.ToString() });
        }
    }

使用上面的代码,手风琴的标题工作正常,但我只获得内容中的ListView数据:System.Data.DataRowView

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您错过了致电:

DataBinder.Eval(container.DataItem, "YouBindingProperty");

示例:

public void DataBinding(object sender, EventArgs e)
{
    var genericControl = (HtmlGenericControl)sender;
    var container = (ListViewDataItem)container.NamingContainer;

    container.Controls.Add(new Literal() { Text = DataBinder.Eval(container.DataItem, "YouBindingProperty") });
}