Reputation: 14256
I am trying to implement a ListView data control for displaying and editing lookup table/ application level variables. There are multiple entity classes which can be bound to the ListView, so the ItemTemplate needs to be dynamically bound to the selected entity object.
For example i have:
AddressType { AddressTypeId, AddressTypeDescription},
PhoneType { PhoneTypeId, PhoneType},
MarriageStatusType { MarriageStatusId, marriageStatusType}
Those generated entity objects prevent me from simply doing something like the following snippet, because the ID and Type properties are different on each business object.
<ListView>
...
<itemTemplate>
<tr>
<td runat="server" id="tdId"> <%# Eval("ID") %> </td>
<td runat="server" id="tdType"> <%# Eval("TypeNameDescription") %> </td>
</tr>
</itemTemplate>
...
</ListView>
I am trying to discover : 1. How to iterate over the ListView Items to insert the appropriate property value into the server side html td tags. 2. How to use Databinder.Eval on the ListView items to insert that property value.
Thanks in advance!
Upvotes: 2
Views: 13511
Reputation: 1
Ok, I find way to render repeater inside the listview, I don't know if I can paste the whole code because it is pretty long. the result html like following:
The hard part is that the number of group can be different
my aspx page like following:
...hm...the forum I cannot use verbatim for code, the pre block won't work for some character
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);
}
}
Upvotes: 0
Reputation: 23935
Ok in answer to your questions:
Hope it helps..
<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>
Upvotes: 4
Reputation: 18572
maybe your answer is to bind a repeater inside the itemTemplate
and the repeater will get the datasource of the <%# Eval("DataDictionary") %>.
Upvotes: 0