0

我创建了一个简单的 ASPX 页面,向页面添加了 ListView 控件并创建了 ITemplate 的两个实现;一个用于 LayoutTemplate,一个用于 ItemTemplate。这里的诀窍是我指定了我想在 ListView 中显示的列的集合,并将该集合作为参数提供给我的 LayoutTemplate 类。这是整个代码(在“LoadDataSource”中对“Columns.Skip(1)”的调用只是为了验证我可以使用任意数量的列呈现 ListView):

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();
        }
    }
}

查看具有不同列数的 ListView 没有问题,但是每当我单击标题中的链接时,我都会收到此服务器错误,而不是 MethodNotImplemented:

必须在 ListView 'MyListView' 上指定项目占位符...

我认为答案很简单,但我就是找不到原因。有没有人有什么建议?

4

1 回答 1

0

好的,我找到了发生这种情况的原因 - 在下面添加了代码......

首先,在 MyItemTemplate->MyTableRow_DataBinding 中,我使用 FindControl 加载控件并为其分配值。这些控件已添加到 InstantiateIn 方法中。显然,这不是正确的方法。

相反,您需要在 DataBinding 事件中创建控件,分配正确的值并将它们添加到 MyHtmlTableRow(sender 参数)。

其次,我需要能够呈现更复杂的表格设计(包括thead 和tbody),所以我用HtmlGenericControl 替换了HtmlTable、HtmlTableRow 和HtmlTableCell 的使用,并将标签名称作为参数传递,如下所示:

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);
        }
    }
}

请注意,您可能可以使用除 HtmlGenericControl 之外的其他服务器控件,例如 Table、TableHeaderRow、TableHeaderCell 等,也就是说,在这种情况下它可能没有任何区别,但主要问题已解决。

于 2011-02-08T12:58:38.993 回答