How to define listview templates in code(如何在代码中定义列表视图模板)
问题描述
我正在编写一个复合控件,其中包含一个用于显示项目表的列表视图.通常在 Asp.NET 中使用 ListView 时,我会在代码转发中定义模板.
<布局模板><div class="ContentContainer"><div runat="server" id="itemPlaceholder"/></LayoutTemplate><项目模板><div><div><%#Eval("Content")%></div>
</ItemTemplate></asp:ListView>
我认为是这样的:
ListView view = new ListView();view.LayoutTemplate = .....view.ItemTemplate = .....//我什么时候调用这些?view.DataSource = myDataSource;视图.DataBind();
更新:我通过实现 ITemplate 接口创建了 2 个模板:
私有类 LayoutTemplate : ITemplate{public void InstantiateIn(控制容器){var external = new HtmlGenericControl("div");var inner = new HtmlGenericControl("div") { ID = "itemPlaceholder" };table.Rows.Add(row);container.Controls.Add(table);}}私有类 ItemTemplate : ITemplate{public void InstantiateIn(控制容器){var inner = new HtmlGenericControl("div");container.Controls.Add(inner);}}
我可以添加它们:
dataList.LayoutTemplate = new LayoutTemplate();dataList.ItemTemplate = new ItemTemplate();
但后来我卡住了,因为 container.DataItem 为空.
诀窍是订阅 ItemTemplate 中 itemplaceholder 的数据绑定事件.
完整的解决方案:
公共类 FibonacciControl : CompositeControl{公共斐波那契控制(){//....}受保护的覆盖无效 CreateChildControls(){base.CreateChildControls();ListView view = new ListView();view.LayoutTemplate = new LayoutTemplate();view.ItemTemplate = new ItemTemplate();view.DataSource = FibonacciSequence();视图.DataBind();this.Controls.Add(view);}私有 IEnumerable斐波那契数列(){int i1 = 0;int i2 = 1;for (int i = 0; i < 迭代次数; i++){收益率收益 i1 + i2;int temp = i1 + i2;i1 = i2;i2 = 温度;}产量中断;}公共 int 迭代 { 获取;放;}私有类 LayoutTemplate : ITemplate{public void InstantiateIn(控制容器){var ol = new HtmlGenericControl("ol");var li = new HtmlGenericControl("li") { ID = "itemPlaceholder" };ol.Controls.Add(li);container.Controls.Add(ol);}}私有类 ItemTemplate : ITemplate{public void InstantiateIn(控制容器){var li = new HtmlGenericControl("li");li.DataBinding += 数据绑定;container.Controls.Add(li);}公共无效数据绑定(对象发送者,EventArgs e){var 容器 = (HtmlGenericControl)sender;var dataItem = ((ListViewDataItem)container.NamingContainer).DataItem;container.Controls.Add( new Literal(){Text = dataItem.ToString() });}}}
I am writing a Composite control, which contains a listview to display a table of items. Normally when using a ListView in Asp.NET I would define the templates in the code-forward.
<asp:ListView runat="server" ID="ArticleList">
<LayoutTemplate>
<div class="ContentContainer">
<div runat="server" id="itemPlaceholder" />
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<div><%# Eval("Content") %></div>
</div>
</ItemTemplate>
</asp:ListView>
I assume it's something like:
ListView view = new ListView();
view.LayoutTemplate = .....
view.ItemTemplate = .....
// when do I call these?
view.DataSource = myDataSource;
view.DataBind();
Update: I created 2 templates by implementing the ITemplate interface:
private class LayoutTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
var outer = new HtmlGenericControl("div");
var inner = new HtmlGenericControl("div") { ID = "itemPlaceholder" };
table.Rows.Add(row);
container.Controls.Add(table);
}
}
private class ItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
var inner = new HtmlGenericControl("div");
container.Controls.Add(inner);
}
}
and I can add them using:
dataList.LayoutTemplate = new LayoutTemplate();
dataList.ItemTemplate = new ItemTemplate();
But then I get stuck, since container.DataItem is null.
The trick is to subscribe to the databinding event of the itemplaceholder in the ItemTemplate.
The complete solution:
public class FibonacciControl : CompositeControl
{
public FibonacciControl()
{
// ....
}
protected override void CreateChildControls()
{
base.CreateChildControls();
ListView view = new ListView();
view.LayoutTemplate = new LayoutTemplate();
view.ItemTemplate = new ItemTemplate();
view.DataSource = FibonacciSequence();
view.DataBind();
this.Controls.Add(view);
}
private IEnumerable<int> FibonacciSequence()
{
int i1 = 0;
int i2 = 1;
for (int i = 0; i < Iterations; i++)
{
yield return i1 + i2;
int temp = i1 + i2;
i1 = i2;
i2 = temp;
}
yield break;
}
public int Iterations { get; set; }
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() });
}
}
}
这篇关于如何在代码中定义列表视图模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在代码中定义列表视图模板
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30