I have a method that creates table and then creates a repeater right after, the table gets rendered but the repeater just does not get rendered. The method below simply creates a table first, filling it with information then dynamically constructs a repeater which works fine, but then it just does not render the repeater onto the aspx page. I have tried using the stringbuilder to return it as a string but still doesn't work. here is the code below. Thanks
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();
}
You cannot render Repeater control as a string. Instead, you need to add the Repeater to dvUserExperience as a control.
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;
}
You need to add controls to RepeaterItem which is rptItem.
See the arrows <=====
in the following code.
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;
}