Creating an ASP.NET Repeater Dynamically in C# Bound to an Object List
I've got a very simple object:
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; }
}
}
I've got a list of DocumentType objects: List<DocumentType> documentTypes = getDocuments();
I'm working on a custom control where I'm trying to dynamically create a repeater and dynamically bind it to my object list. Here's my code:
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 Dynami开发者_开发知识库cally
repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);
LiteralControl ltrlHR = new LiteralControl();
ltrlHR.Text = "<hr />";
repeatItem.Controls.Add(ltrlHR);
}
The header and separator work great. I can't figure out how to bind the item template to the current item to get it to display. I know what I have in there right now is completely broken, but I've tried several variations with no luck.
Thanks in advance for any help or pointers in the right direction!
The problem you are having is that you are assuming that the RepeaterItem contains the data. It does not. It contains the information on how to display the individual item. You need to use that index to get back into the data source. I'm not sure if there's a better way, but below is how I got it to work...
List<DocumentType> documentTypes = new List<DocumentType>();
documentTypes.Add(new DocumentType(){ ID=1, Name="Bob"});
documentTypes.Add(new DocumentType() { ID = 2, Name = "Tom" });
documentTypes.Add(new DocumentType() { ID = 3, Name = "Chick" });
Repeater docList = new Repeater();
docList.DataSource = documentTypes;
docList.DataBind();
foreach (RepeaterItem repeatItem in docList.Items)
{
int index = repeatItem.ItemIndex;
DocumentType docType = ((IList<DocumentType>)docList.DataSource)[index];
// if condition to add HeaderTemplate Dynamically only Once
if (index == 0)
{
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 />", docType.ID, repeaterItem.NamingContainer);
repeatItem.Controls.Add(lbl);
// Add SeparatorTemplate Dynamically
LiteralControl ltrlHR = new LiteralControl();
ltrlHR.Text = "<hr />";
repeatItem.Controls.Add(ltrlHR);
}
StringBuilder sb = new StringBuilder();
docList.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
Text = sb.ToString();
精彩评论