2008-12-23 50 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); 
} 

}