如何从代码后面动态添加Repeater控件?

时间:2014-08-14 23:19:59

标签: c# asp.net repeater

我有一个创建表的方法,然后在之后创建一个转发器,表被渲染但转发器不会被渲染。下面的方法首先简单地创建一个表,用信息填充它然后动态构造一个工作正常的转发器,但它只是不会将转发器呈现到aspx页面上。我已经尝试使用stringbuilder将其作为字符串返回,但仍然无法正常工作。这是下面的代码。谢谢

private void CreateUserExperienceTable(List<UserExperience> experiences)
    {
        foreach (UserExperience experience in experiences)
        {
            HtmlGenericControl Header = new HtmlGenericControl("h3");
            Header.InnerHtml = experience.Company;
            dvUserExperience.Controls.Add(Header);

            Table experienceTable = new Table();

            TableRow experienceRoleRow = new TableRow();
            TableRow experienceDescriptionRow = new TableRow();
            TableRow experiencePeriodFromRow = new TableRow();
            TableRow experiencePeriodToRow = new TableRow();

            TableCell experienceRoleTitleCell = new TableCell();
            TableCell experienceRoleValueCell = new TableCell();
            TableCell experienceDescriptionTitleCell = new TableCell();
            TableCell experienceDescriptionValueCell = new TableCell();
            TableCell experiencePeriodFromTitleCell = new TableCell();
            TableCell experiencePeriodFromValueCell = new TableCell();
            TableCell experiencePeriodToTitleCell = new TableCell();
            TableCell experiencePeriodToValueCell = new TableCell();

            experienceRoleTitleCell.Text = "Role:";
            experienceRoleValueCell.Text = experience.Role;
            experienceDescriptionTitleCell.Text = "Description:";
            experienceDescriptionValueCell.Text = experience.CompanyDescription;
            experiencePeriodFromTitleCell.Text = "Period From: ";
            experiencePeriodFromValueCell.Text = experience.PeriodFrom.ToString("yy-mm-dd");
            experiencePeriodToTitleCell.Text = "Period To:";
            experiencePeriodToValueCell.Text = experience.PeriodTo == null
                ? "Present"
                : experience.PeriodTo.ToString();

            experienceRoleRow.Cells.Add(experienceRoleTitleCell);
            experienceRoleRow.Cells.Add(experienceRoleValueCell);
            experienceDescriptionRow.Cells.Add(experienceDescriptionTitleCell);
            experienceDescriptionRow.Cells.Add(experienceDescriptionValueCell);
            experiencePeriodFromRow.Cells.Add(experiencePeriodFromTitleCell);
            experiencePeriodFromRow.Cells.Add(experiencePeriodFromValueCell);
            experiencePeriodToRow.Cells.Add(experiencePeriodToTitleCell);
            experiencePeriodToRow.Cells.Add(experiencePeriodToValueCell);

            experienceTable.Rows.Add(experienceRoleRow);
            experienceTable.Rows.Add(experienceDescriptionRow);
            experienceTable.Rows.Add(experiencePeriodFromRow);
            experienceTable.Rows.Add(experiencePeriodToRow);

            dvUserExperience.Controls.Add(experienceTable);

            String rptDuties = updatePageWithDuties(experience.Duties);
            //dvUserExperience.Controls.Add(rptDuties);
        }
    }

    private string updatePageWithDuties(List<ExperienceDuties> list)
    {
        Repeater rptDuties = new Repeater();
        rptDuties.DataSource = list;
        rptDuties.DataBind();

        foreach (RepeaterItem rptItem in rptDuties.Items)
        {
            if (rptItem.ItemIndex == 0)
            {
                RepeaterItem headerTemplate = new RepeaterItem(rptItem.ItemIndex, ListItemType.Header);
                HtmlGenericControl h4Tag = new HtmlGenericControl("h4");
                h4Tag.InnerHtml = "Duties";
                headerTemplate.Controls.Add(h4Tag);
            }

            RepeaterItem itemTemplate = new RepeaterItem(rptItem.ItemIndex, ListItemType.Item);
            Label dutyLabel = new Label();
            ExperienceDuties expDuties = ((IList<ExperienceDuties>)rptDuties.DataSource)[rptItem.ItemIndex];
            dutyLabel.Text = expDuties.Description;
            itemTemplate.Controls.Add(dutyLabel);

            RepeaterItem seperatorItem = new RepeaterItem(rptItem.ItemIndex, ListItemType.Separator);
            LiteralControl ltrHR = new LiteralControl();
            ltrHR.Text = "<hr />";
            seperatorItem.Controls.Add(ltrHR);
        }

        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter writer = new HtmlTextWriter(sw);

        rptDuties.RenderControl(writer);
        return sb.ToString();
    }

2 个答案:

答案 0 :(得分:1)

您无法将 Repeater 控件呈现为字符串。相反,您需要将转发器添加到 dvUserExperience 作为控件。

private void CreateUserExperienceTable(List<UserExperience> experiences)
{
    foreach (UserExperience experience in experiences)
    {
        ...

        dvUserExperience.Controls.Add(experienceTable);

        // Add as a server control
        Repeater rptDuties = updatePageWithDuties(experience.Duties);
        dvUserExperience.Controls.Add(rptDuties);
    }
}

private Repeater updatePageWithDuties(List<ExperienceDuties> list)
{
    Repeater rptDuties = new Repeater();
    ...

    return rptDuties;
}

更新

您需要将控件添加到 RepeaterItem ,即 rptItem

请参阅以下代码中的箭头<=====

private Repeater updatePageWithDuties(List<ExperienceDuties> list)
{
    Repeater rptDuties = new Repeater();
    rptDuties.DataSource = list;
    rptDuties.DataBind();

    foreach (RepeaterItem rptItem in rptDuties.Items)
    {
        if (rptItem.ItemIndex == 0)
        {
            var h4Tag = new HtmlGenericControl("h4");
            h4Tag.InnerHtml = "Duties";
            rptItem.Controls.Add(h4Tag);  <=====
        }

        var dutyLabel = new Label();
        ExperienceDuties expDuties = 
          ((IList<ExperienceDuties>) rptDuties.DataSource)[rptItem.ItemIndex];
        dutyLabel.Text = expDuties.Description;
        rptItem.Controls.Add(dutyLabel);   <=====

        var ltrHR = new LiteralControl();
        ltrHR.Text = "<hr />";
        rptItem.Controls.Add(ltrHR);  <=====
    }

    return rptDuties;
}

答案 1 :(得分:0)

您的转发器不在Form控件集合中。应该在表格中显示它。您必须使用this.Controls.Add(rptDuties);将转发器添加到表单的控件集合中,而不是返回StringBuilder对象。这应该会呈现您的转发器。

更新 - 使用工作单元 如果您想知道改变的地方,这是一个有效的样本。

注意:@Win的回答是有效的。您应该这样做并将您的中继器添加到占位符,即在这种情况下的dvUserExperience DIV。

标记代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestRepeater.aspx.cs" Inherits="ExcelUpload.TestRepeater" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="dvUserExperience" runat="server">

    </div>
    </form>
</body>
</html>

背后的代码

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace ExcelUpload
{
    public partial class TestRepeater : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CreateUserExperienceTable(LoadData());
        }

        private List<UserExperience> LoadData()
        {
            List<UserExperience> experiences = new List<UserExperience>();

            for (int i = 0; i < 5; i++)
            {
                UserExperience newExp = new UserExperience();
                newExp.Company = "Company " + i;
                newExp.Role = "Role " + i;
                newExp.CompanyDescription = "CompanyDescription " + i;
                newExp.PeriodFrom = DateTime.Now.AddDays(i);
                newExp.PeriodTo = DateTime.Now.AddDays(i + 5);

                for (int j = 0; j < 2; j++)
                {
                    ExperienceDuties newDuty = new ExperienceDuties();
                    newDuty.Description = "Duty Description " + j;
                    newExp.Duties.Add(newDuty);
                }

                experiences.Add(newExp);
            }

            return experiences;
        }

        private void CreateUserExperienceTable(List<UserExperience> experiences)
        {
            foreach (UserExperience experience in experiences)
            {
                HtmlGenericControl Header = new HtmlGenericControl("h3");
                Header.InnerHtml = experience.Company;
                dvUserExperience.Controls.Add(Header);

                Table experienceTable = new Table();

                TableRow experienceRoleRow = new TableRow();
                TableRow experienceDescriptionRow = new TableRow();
                TableRow experiencePeriodFromRow = new TableRow();
                TableRow experiencePeriodToRow = new TableRow();

                TableCell experienceRoleTitleCell = new TableCell();
                TableCell experienceRoleValueCell = new TableCell();
                TableCell experienceDescriptionTitleCell = new TableCell();
                TableCell experienceDescriptionValueCell = new TableCell();
                TableCell experiencePeriodFromTitleCell = new TableCell();
                TableCell experiencePeriodFromValueCell = new TableCell();
                TableCell experiencePeriodToTitleCell = new TableCell();
                TableCell experiencePeriodToValueCell = new TableCell();

                experienceRoleTitleCell.Text = "Role:";
                experienceRoleValueCell.Text = experience.Role;
                experienceDescriptionTitleCell.Text = "Description:";
                experienceDescriptionValueCell.Text = experience.CompanyDescription;
                experiencePeriodFromTitleCell.Text = "Period From: ";
                experiencePeriodFromValueCell.Text = experience.PeriodFrom.ToString("yy-mm-dd");
                experiencePeriodToTitleCell.Text = "Period To:";
                experiencePeriodToValueCell.Text = experience.PeriodTo == null
                    ? "Present"
                    : experience.PeriodTo.ToString();

                experienceRoleRow.Cells.Add(experienceRoleTitleCell);
                experienceRoleRow.Cells.Add(experienceRoleValueCell);
                experienceDescriptionRow.Cells.Add(experienceDescriptionTitleCell);
                experienceDescriptionRow.Cells.Add(experienceDescriptionValueCell);
                experiencePeriodFromRow.Cells.Add(experiencePeriodFromTitleCell);
                experiencePeriodFromRow.Cells.Add(experiencePeriodFromValueCell);
                experiencePeriodToRow.Cells.Add(experiencePeriodToTitleCell);
                experiencePeriodToRow.Cells.Add(experiencePeriodToValueCell);

                experienceTable.Rows.Add(experienceRoleRow);
                experienceTable.Rows.Add(experienceDescriptionRow);
                experienceTable.Rows.Add(experiencePeriodFromRow);
                experienceTable.Rows.Add(experiencePeriodToRow);

                dvUserExperience.Controls.Add(experienceTable);

                updatePageWithDuties(experience.Duties);
            }
        }

        private void updatePageWithDuties(List<ExperienceDuties> list)
        {
            Repeater rptDuties = new Repeater();
            rptDuties.ID = "rptDuties";
            rptDuties.DataSource = list;
            rptDuties.DataBind();

            foreach (RepeaterItem rptItem in rptDuties.Items)
            {
                if (rptItem.ItemIndex == 0)
                {
                    RepeaterItem headerTemplate = new RepeaterItem(rptItem.ItemIndex, ListItemType.Header);
                    HtmlGenericControl h4Tag = new HtmlGenericControl("h4");
                    h4Tag.InnerHtml = "Duties";
                    rptItem.Controls.Add(h4Tag);
                }

                RepeaterItem itemTemplate = new RepeaterItem(rptItem.ItemIndex, ListItemType.Item);
                Label dutyLabel = new Label();
                ExperienceDuties expDuties = ((IList<ExperienceDuties>)rptDuties.DataSource)[rptItem.ItemIndex];
                dutyLabel.Text = expDuties.Description;
                rptItem.Controls.Add(dutyLabel);

                RepeaterItem seperatorItem = new RepeaterItem(rptItem.ItemIndex, ListItemType.Separator);
                LiteralControl ltrHR = new LiteralControl();
                ltrHR.Text = "<hr />";
                rptItem.Controls.Add(ltrHR);
            }

            dvUserExperience.Controls.Add(rptDuties);
        }
    }

    public class UserExperience {
        public List<ExperienceDuties> Duties = new List<ExperienceDuties>();
        public string  Company { get; set; }
        public string Role { get; set; }
        public string CompanyDescription { get; set; }
        public DateTime PeriodFrom { get; set; }
        public DateTime PeriodTo { get; set; }
    }

    public class ExperienceDuties {
        public string Description { get; set; }
    }
}