ID "itemPlaceholder" is not rendered when using custom LayoutTemplate

968 views Asked by At

I've created a simple ASPX-page, added a ListView control to the page and created two implementations of the ITemplate; one for the LayoutTemplate and one for the ItemTemplate. The trick here is that I specify a collection of columns I want to display in the ListView and supplying the collection as a parameter to my LayoutTemplate class. Here's the whole code (the call to "Columns.Skip(1)" in "LoadDataSource" is merely to verify that I can render the ListView with a arbitrary number of columns):

public partial class ListViewITemplate : System.Web.UI.Page
{
    private static IList<string> Columns
    {
        get
        {
            return new List<string>() { "ColumnA", "ColumnB", "ColumnC", "ColumnD", "ColumnE" };
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dataTable = LoadDataSource();

        IEnumerable<string> columns = dataTable.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
        MyListView.LayoutTemplate = new MyLayoutTemplate(columns);
        MyListView.ItemTemplate = new MyItemTemplate(columns);
        MyListView.DataSource = dataTable;
        MyListView.DataBind();
    }

    private static DataTable LoadDataSource()
    {
        DataTable dataTable = new DataTable();
        IEnumerable<string> selectedColumns = Columns.Skip(1);
        foreach (string column in selectedColumns)
        {
            dataTable.Columns.Add(column, typeof(string));
        }

        for (int i = 0; i < 100; i++)
        {
            DataRow dataRow = dataTable.NewRow();
            foreach (string column in selectedColumns)
            {
                dataRow[column] = "Data in " + column + " " + i;
            }

            dataTable.Rows.Add(dataRow);
        }
        return dataTable;
    }
}

public class MyLayoutTemplate : ITemplate
{
    private IEnumerable<string> Columns { get; set; }

    public MyLayoutTemplate(IEnumerable<string> columns)
    {
        Columns = columns;
    }

    public void InstantiateIn(Control container)
    {
        HtmlTable MyHtmlTable = new HtmlTable();
        HtmlTableRow MyHtmlTableRow = new HtmlTableRow();
        MyHtmlTableRow.Attributes["class"] = "tableheader";
        foreach (string column in Columns)
        {
            HtmlTableCell MyHtmlTableCell = new HtmlTableCell();
            LinkButton MyLinkButton = new LinkButton();

            MyLinkButton.ID = "lbn" + column;
            MyLinkButton.Text = column;
            MyLinkButton.ToolTip = "Sort by " + column;
            MyLinkButton.CommandArgument = column;
            MyLinkButton.Command += new CommandEventHandler(MyLinkButton_Command);
            MyHtmlTableCell.Controls.Add(MyLinkButton);
            MyHtmlTableRow.Cells.Add(MyHtmlTableCell);
        }
        MyHtmlTable.Rows.Add(MyHtmlTableRow);
        HtmlTableRow htmlTableRow = new HtmlTableRow();
        htmlTableRow.ID = "itemPlaceholder";
        MyHtmlTable.Rows.Add(htmlTableRow);

        container.Controls.Add(MyHtmlTable);
    }

    void MyLinkButton_Command(object sender, CommandEventArgs e)
    {
        throw new NotImplementedException();
    }
}

public class MyItemTemplate : ITemplate
{
    private IEnumerable<string> Columns { get; set; }

    public MyItemTemplate(IEnumerable<string> columns)
    {
        Columns = columns;
    }

    public void InstantiateIn(Control container)
    {
        HtmlTableRow MyHtmlTableRow = new HtmlTableRow();

        foreach (string column in Columns)
        {
            HtmlTableCell MyHtmlTableCell = new HtmlTableCell();
            MyHtmlTableCell.ID = "MyHtmlTableCell" + column;
            Literal MyLiteral = new Literal();
            MyLiteral.ID = "Data" + column;
            MyHtmlTableCell.Controls.Add(MyLiteral);
            MyHtmlTableRow.Cells.Add(MyHtmlTableCell);
        }
        MyHtmlTableRow.DataBinding += new EventHandler(MyTableRow_DataBinding);
        container.Controls.Add(MyHtmlTableRow);
    }

    protected void MyTableRow_DataBinding(object sender, EventArgs e)
    {
        HtmlTableRow MyHtmlTableRow = (HtmlTableRow)sender;
        DataRowView dataRowView = ((ListViewDataItem)MyHtmlTableRow.NamingContainer).DataItem as DataRowView;
        foreach (string column in Columns)
        {
            HtmlTableCell MyHtmlTableCell = (HtmlTableCell)MyHtmlTableRow.FindControl("MyHtmlTableCell" + column);
            Literal MyLiteral = (Literal)MyHtmlTableCell.FindControl("Data" + column);
            MyLiteral.Text = dataRowView[column].ToString();
        }
    }
}

I have no problem viewing the ListView with varying number of columns, but whenever I click the link in the header I get this server error instead of MethodNotImplemented:

An item placeholder must be specified on ListView 'MyListView'...

I think the answer is straightforward but I just can't find the cause of this. Does anyone have any suggestions?

1

There are 1 answers

0
Stig Perez On BEST ANSWER

Okay, I found out why this happened - code is added below...

First of all, in MyItemTemplate->MyTableRow_DataBinding I'm using FindControl to load the controls and assign values to them. These controls were already added in the InstantiateIn method. Apparently, that's not the right way.

Instead you need to create the controls in the DataBinding event, assign the correct values and add them to the MyHtmlTableRow (the sender parameter).

Secondly, I needed to be able to render a more complex table design (including thead and tbody) so I replaced the use of HtmlTable, HtmlTableRow and HtmlTableCell with HtmlGenericControl and passed the tag name as parameter, like so:

public partial class ListViewITemplate : System.Web.UI.Page
{
    private static IList<string> Columns
    {
        get
        {
            return new List<string>() { "ColumnA", "ColumnB", "ColumnC", "ColumnD", "ColumnE" };
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dataTable = LoadDataSource();

        IEnumerable<string> columns = dataTable.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
        MyListView.LayoutTemplate = new MyLayoutTemplate(columns);
        MyListView.ItemTemplate = new MyItemTemplate(columns);
        MyListView.DataSource = dataTable;
        MyListView.DataBind();
    }

    private static DataTable LoadDataSource()
    {
        DataTable dataTable = new DataTable();
        IEnumerable<string> selectedColumns = Columns.Skip(1);
        foreach (string column in selectedColumns)
        {
            dataTable.Columns.Add(column, typeof(string));
        }

        for (int i = 0; i < 100; i++)
        {
            DataRow dataRow = dataTable.NewRow();
            foreach (string column in selectedColumns)
            {
                dataRow[column] = "Data in " + column + " " + i;
            }

            dataTable.Rows.Add(dataRow);
        }
        return dataTable;
    }
}

public class MyLayoutTemplate : ITemplate
{
    private IEnumerable<string> Columns { get; set; }

    public MyLayoutTemplate(IEnumerable<string> columns)
    {
        Columns = columns;
    }

    public void InstantiateIn(Control container)
    {
        HtmlGenericControl MyHtmlTable = new HtmlGenericControl("table");
        HtmlGenericControl MyHtmlTableHead = new HtmlGenericControl("thead");
        HtmlGenericControl MyHtmlTableRow = new HtmlGenericControl("tr");
        foreach (string column in Columns)
        {
            HtmlGenericControl MyHtmlTableCell = new HtmlGenericControl("th");
            LinkButton MyLinkButton = new LinkButton();

            MyLinkButton.ID = "lbn" + column;
            MyLinkButton.Text = column;
            MyLinkButton.ToolTip = "Sort by " + column;
            MyLinkButton.CommandArgument = column;
            MyLinkButton.Command += new CommandEventHandler(MyLinkButton_Command);
            MyHtmlTableCell.Controls.Add(MyLinkButton);
            MyHtmlTableRow.Controls.Add(MyHtmlTableCell);
        }
        MyHtmlTableHead.Controls.Add(MyHtmlTableRow);
        MyHtmlTable.Controls.Add(MyHtmlTableHead);
        HtmlGenericControl MyHtmlTableBody = new HtmlGenericControl("tbody");
        HtmlGenericControl MyHtmlItemPlaceholderRow = new HtmlGenericControl("tr");
        MyHtmlItemPlaceholderRow.ID = "itemPlaceholder";
        MyHtmlTableBody.Controls.Add(MyHtmlItemPlaceholderRow);
        MyHtmlTable.Controls.Add(MyHtmlTableBody);

        container.Controls.Add(MyHtmlTable);
    }

    protected void MyLinkButton_Command(object sender, CommandEventArgs e)
    {
        throw new NotImplementedException();
    }
}

public class MyItemTemplate : ITemplate
{
    private IEnumerable<string> Columns { get; set; }

    public MyItemTemplate(IEnumerable<string> columns)
    {
        Columns = columns;
    }

    public void InstantiateIn(Control container)
    {
        HtmlGenericControl MyHtmlTableRow = new HtmlGenericControl("tr");

        MyHtmlTableRow.DataBinding += new EventHandler(MyTableRow_DataBinding);
        container.Controls.Add(MyHtmlTableRow);
    }

    protected void MyTableRow_DataBinding(object sender, EventArgs e)
    {
        HtmlGenericControl MyHtmlTableRow = (HtmlGenericControl)sender;
        DataRowView dataRowView = ((ListViewDataItem)MyHtmlTableRow.NamingContainer).DataItem as DataRowView;
        foreach (string column in Columns)
        {
            HtmlGenericControl MyHtmlTableCell = new HtmlGenericControl("td");
            MyHtmlTableCell.ID = "MyHtmlTableCell" + column;
            Literal MyLiteral = new Literal();
            MyLiteral.ID = "Data" + column;
            MyLiteral.Text = dataRowView[column].ToString();
            MyHtmlTableCell.Controls.Add(MyLiteral);
            MyHtmlTableRow.Controls.Add(MyHtmlTableCell);
        }
    }
}

As a note you could probably use other server controls than HtmlGenericControl, e.g. Table, TableHeaderRow, TableHeaderCell etc., that is to say it probably didn't make any difference in this case but the main issue is solved.