2008-12-23 76 views
2

我想实现显示和编辑查找表/应用程序级变量一个ListView数据控制。有多个实体类可以绑定到ListView,因此需要将ItemTemplate动态绑定到选定的实体对象。Asp.NET的ListView数据控制动态数据绑定

,比如我有:

AddressType { AddressTypeId, AddressTypeDescription}, 
PhoneType { PhoneTypeId, PhoneType}, 
MarriageStatusType { MarriageStatusId, marriageStatusType} 

这些生成的实体对象阻止我只做类似于下面的代码片段,因为ID和类型属性是每个业务对象上不同。

<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来插入该属性值。

在此先感谢!

回答

4

在回答好你的问题:

  • 您可以通过在父ListView中使用OnItemDataBound事件在ListViewItems迭代。然后,您可以使用此数据绑定嵌套子列表视图(或插入HTML或操纵列表的内容在任何你需要的方式)。请确保您使用ListViewItemEventArgs在后面的代码处理程序,以便您可以轻松地访问数据绑定项...
  • 可以使用的DataBinder.Eval使用这样的事情(注意“GetChildCategoryData”动态填充一个转发器在你的ListView是一个代码后面的方法):

希望它可以帮助..

<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> 
0

也许你的答案是一个中继器绑定的ItemTemplate

和中继内将得到<%#的eval(“数据字典”)的数据源%>。

0

好吧,我找到办法来渲染列表视图内转发,我不知道我是否可以粘贴整个代码,因为它是相当长。结果HTML类似如下:

 


            
 
  • time1 [comment: following is repeater inside listview] scenarioNamegroupName1groupName2 S1g1Conc1g2Conc1 S2g1Conc2g2Conc2 S3g1Conc1g2Conc3
  • time2 [comment: following is repeater inside listview] scenarioNamegroupName1groupName2 S1g1Conc1g2Conc1 S2g1Conc2g2Conc2 S3g1Conc1g2Conc3
  • time3 [comment: following is repeater inside listview] scenarioNamegroupName1groupName2 S1g1Conc1g2Conc1 S2g1Conc2g2Conc2 S3g1Conc1g2Conc3

难的是该组的数量可以是不同的

我的aspx页面类似以下内容:

...唔...的论坛上,我不能逐字使用代码,预块不会为一些字符

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工作; 公共MyTemplate的(System.Web.UI.WebControls.ListItemType型) { templateType =类型; }

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>"[email protected]"</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); 
} 

}