我正在尝试实现一个ListView数据控件来显示和编辑查找表/应用程序级变量。有多个实体类可以绑定到ListView,因此需要将ItemTemplate动态绑定到选定的实体对象。
例如我有:
AddressType { AddressTypeId, AddressTypeDescription},
PhoneType { PhoneTypeId, PhoneType},
MarriageStatusType { MarriageStatusId, marriageStatusType}
这些生成的实体对象阻止我简单地执行类似以下代码段的操作,因为每个业务对象的ID和Type属性都不同。
<ListView>
...
<itemTemplate>
<tr>
<td runat="server" id="tdId"> <%# Eval("ID") %> </td>
<td runat="server" id="tdType"> <%# Eval("TypeNameDescription") %> </td>
</tr>
</itemTemplate>
...
</ListView>
我试图发现: 1.如何迭代ListView项以将适当的属性值插入服务器端html td标记。 2.如何在ListView项目上使用Databinder.Eval来插入该属性值。
提前致谢!
答案 0 :(得分:4)
好的回答你的问题:
希望有所帮助......
<asp:ListView ID="parentList" runat="server">
<ItemTemplate>
<asp:Repeater ID="childData" runat="server" DataSource='<%# GetChildCategoryData(DataBinder.Eval(Container.DataItem, "parentcategoryID")) %>'>.. </asp:Repeater>
</ItemTemplate>
</asp:ListView>
答案 1 :(得分:0)
也许你的答案是在itemTemplate
中绑定一个转发器并且转发器将获取&lt;%#Eval(“DataDictionary”)%&gt;的数据源。
答案 2 :(得分:0)
好的,我找到了在listview中渲染转发器的方法,我不知道是否可以粘贴整个代码,因为它很长。结果html如下:
困难的部分是组的数量可以不同
我的aspx页面如下:
......嗯...论坛我不能逐字逐句地使用代码,pre block对某些字符不起作用
KeyValuePair<string, List<scenarioItem>> myData = (KeyValuePair<string, List<scenarioItem>>)(((System.Web.UI.WebControls.ListViewDataItem)(e.Item)).DataItem);
Repeater repeater = (Repeater)e.Item.FindControl("childData");
repeater.HeaderTemplate = new MyTemplate(ListItemType.Header);
repeater.ItemTemplate = new MyTemplate(ListItemType.Item);
repeater.AlternatingItemTemplate =
new MyTemplate(ListItemType.AlternatingItem);
repeater.FooterTemplate = new MyTemplate(ListItemType.Footer);
repeater.DataSource = myData.Value;
repeater.DataBind();
public class MyTemplate : System.Web.UI.ITemplate
{ System.Web.UI.WebControls.ListItemType templateType; public MyTemplate(System.Web.UI.WebControls.ListItemType type) { templateType = type; }
static void Item_DataBinding(object sender, System.EventArgs e)
{
PlaceHolder ph = (PlaceHolder)sender;
RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
scenarioItem myDataItem = (scenarioItem)ri.DataItem;
if (ri.ItemIndex == 0) {
//create the header column part once
//Add ScenarioName
ph.Controls.Add(new LiteralControl("<tr><td>ScenarioName</td>"));
//Add group concentration part
foreach (recGroupConcItem concItem in myDataItem.mGroupConcList)
{
ph.Controls.Add(new LiteralControl("<td>" + concItem.groupName + @"</td>"));
}
ph.Controls.Add(new LiteralControl("</tr>"));
}
//Add ScenarioName
ph.Controls.Add(new LiteralControl("<tr><td>"+myDataItem.scenarioName+@"</td>"));
//Add group concentration part
foreach (recGroupConcItem concItem in myDataItem.mGroupConcList) {
ph.Controls.Add(new LiteralControl("<td>" + concItem.groupConc.ToString() + @"</td>"));
}
ph.Controls.Add(new LiteralControl("</tr>"));
}
static void ItemAlt_DataBinding(object sender, System.EventArgs e)
{
PlaceHolder ph = (PlaceHolder)sender;
RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
scenarioItem myDataItem = (scenarioItem)ri.DataItem;
//Add ScenarioName
ph.Controls.Add(new LiteralControl("<tr bgcolor=\"lightblue\"><td>" + myDataItem.scenarioName + @"</td>"));
//Add group concentration part
foreach (recGroupConcItem concItem in myDataItem.mGroupConcList)
{
ph.Controls.Add(new LiteralControl("<td>" + concItem.groupConc.ToString() + @"</td>"));
}
ph.Controls.Add(new LiteralControl("</tr>"));
}
static void ItemHeader_DataBinding(object sender, System.EventArgs e)
{
PlaceHolder ph = (PlaceHolder)sender;
RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
scenarioItem myDataItem = (scenarioItem)ri.DataItem;
//Add ScenarioName
ph.Controls.Add(new LiteralControl("<tr><td>ScenarioName</td>"));
//Add group concentration part
foreach (recGroupConcItem concItem in myDataItem.mGroupConcList)
{
ph.Controls.Add(new LiteralControl("<td>" + concItem.groupName + @"</td>"));
}
ph.Controls.Add(new LiteralControl("</tr>"));
}
public void InstantiateIn(System.Web.UI.Control container)
{
PlaceHolder ph = new PlaceHolder();
Label item1 = new Label();
Label item2 = new Label();
item1.ID = "item1";
item2.ID = "item2";
switch (templateType)
{
case ListItemType.Header:
ph.Controls.Add(new LiteralControl("<table border=\"1\">"));
// "<tr><td><b>ScenarioName</b></td>" +
// "<td><b>Group1</b></td> <td><b>Group2</b></td> <td><b>Group3</b></td> <td><b>Group4</b></td> </tr>"));
//ph.DataBinding += new EventHandler(ItemHeader_DataBinding);
break;
case ListItemType.Item:
ph.DataBinding += new EventHandler(Item_DataBinding);
break;
case ListItemType.AlternatingItem:
ph.DataBinding += new EventHandler(ItemAlt_DataBinding);
break;
case ListItemType.Footer:
ph.Controls.Add(new LiteralControl("</table>"));
break;
}
container.Controls.Add(ph);
}
}