【问题标题】:Creating an ASP.NET Repeater Dynamically in C# Bound to an Object List在绑定到对象列表的 C# 中动态创建 ASP.NET 中继器
【发布时间】:2017-07-11 05:13:32
【问题描述】:

我有一个非常简单的对象:

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();

我正在开发一个自定义控件,我正在尝试动态创建一个转发器并将其动态绑定到我的对象列表。这是我的代码:

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);
}

标题和分隔符效果很好。我不知道如何将项目模板绑定到当前项目以使其显示。我知道我现在在那里的东西完全坏了,但是我尝试了几种变体,但都没有运气。

提前感谢任何帮助或正确方向的指示!

【问题讨论】:

    标签: c# asp.net repeater


    【解决方案1】:

    您遇到的问题是您假设 RepeaterItem 包含数据。它不是。它包含有关如何显示单个项目的信息。您需要使用该索引返回数据源。我不确定是否有更好的方法,但下面是我如何让它工作...

    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();
    

    【讨论】:

    • John - 谢谢,您的代码运行良好!在我查看的所有示例代码中,我找不到那个 sn-p。我有一种感觉,这是一个简单的任务。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    相关资源
    最近更新 更多