我创建了一个简单的 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' 上指定项目占位符...
我认为答案很简单,但我就是找不到原因。有没有人有什么建议?