3
我有一個非常簡單的對象C#創建ASP.NET直放站動態:綁定到一個對象列表
public class DocumentType
{
private int id;
private string name;
public int ID
{
get { return this.id; }
set { this.id = value; }
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
}
我有DocumentType對象的列表:List<DocumentType> documentTypes = getDocuments();
我m工作在一個自定義控件,我試圖動態創建一箇中繼器,並動態綁定到我的對象列表。這是我的代碼:
private Repeater docList;
docList = new Repeater();
docList.DataSource = documentTypes;
docList.DataBind();
foreach (RepeaterItem repeatItem in docList.Items)
{
// if condition to add HeaderTemplate Dynamically only Once
if (repeatItem.ItemIndex == 0)
{
RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);
HtmlGenericControl hTag = new HtmlGenericControl("h4");
hTag.InnerHtml = "Header";
repeatItem.Controls.Add(hTag);
}
// Add ItemTemplate DataItems Dynamically
RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
Label lbl = new Label();
// This part is completely broken!
lbl.Text = string.Format("Content: {0} {1} <br />", (DocumentType)repeaterItem.DataItem).ID, repeaterItem.NamingContainer);
repeatItem.Controls.Add(lbl);
// Add SeparatorTemplate Dynamically
repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);
LiteralControl ltrlHR = new LiteralControl();
ltrlHR.Text = "<hr />";
repeatItem.Controls.Add(ltrlHR);
}
標題和分隔符很好。我無法弄清楚如何將項目模板綁定到當前項目以使其顯示。我知道我現在在那裏有什麼完全破碎,但我已經嘗試了幾個變化,沒有運氣。
預先感謝任何幫助或指引在正確的方向!
約翰 - 謝謝你,你的代碼工作完美!在我看到的所有示例代碼中,我找不到該代碼段。我有一種感覺,這是一個簡單的任務。再次感謝! – Dexter 2011-04-22 17:10:20