1

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

我究竟做错了什么?

4

1 回答 1

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") });
}
于 2012-07-23T21:58:19.077 回答