I am using c# asp.net and building a SharePoint visual webpart. I have this method which is adding some controls to the page:
private string RSSFile = @"http://www.myCompany.com/xml_feed.xml";
protected override void CreateChildControls()
{
base.CreateChildControls();
// introductionText
HtmlGenericControl introductionText = new HtmlGenericControl("p");
introductionText.InnerHtml = "De volgende vacatures staan momenteel open:";
this.Controls.Add(introductionText);
// table
HtmlGenericControl mainTable = new HtmlGenericControl("table");
mainTable.Attributes.Add("cellpadding", "10");
HtmlGenericControl tr1 = new HtmlGenericControl("tr");
Controls.Add(mainTable);
mainTable.Controls.Add(tr1);
// header columns
tr1.InnerHtml = "<th style=\"text-align:left;\">Vacature</th><th style=\"text-align:left;\">Standplaats</th><th style=\"text-align:left;\">Uren</th>";
// load xml file
// TODO load from webpart property
var xmlSource = XElement.Load(RSSFile);
// get all file elements
IEnumerable<XElement> q =
(from c in xmlSource.Descendants("vacature")
select c).ToArray();
// TODO filter on date
//where (string)c.Element("publish") == "True"
//orderby c.Element("name").Value, c.Element("version").Value
// loop into each file element and get values
foreach (XElement c in q)
{
HtmlGenericControl tr2 = new HtmlGenericControl("tr");
mainTable.Controls.Add(tr2);
// title with hyperlink
HtmlGenericControl td1 = new HtmlGenericControl("td");
td1.Style.Add("vertical-align", "top");
td1.InnerHtml = string.Format("<a href=\"Home.aspx?vacatureNr={0}\">{1}</a>", c.Element("vacaturenr").Value, c.Element("titel").Value);
tr2.Controls.Add(td1);
// location
HtmlGenericControl td2 = new HtmlGenericControl("td");
td2.Style.Add("vertical-align", "top");
td2.InnerHtml = c.Element("regios").Value;
tr2.Controls.Add(td2);
// uren
HtmlGenericControl td3 = new HtmlGenericControl("td");
td3.Style.Add("vertical-align", "top");
td3.InnerHtml = string.Concat(c.Element("min_uren").Value, " - ", c.Element("max_uren").Value);
tr2.Controls.Add(td3);
}
this.Controls.Add(new LiteralControl("<br/>"));
}
Like you see I have an ahref which is referenced to the same page and adds a querystring "vacatureNr". In the pageload I check if this "vacatureNr" is filled. If it is filled I would like to add extra controls to my page. It workds, but the controls are added before the controls which are added in the method "CreateChildControls". How can I add these controls after the controls which are already added in the method "CreateChildControls"?
protected void Page_Load(object sender, EventArgs e)
{
string vacatureNr = Request.QueryString["vacatureNr"];
if(!string.IsNullOrEmpty(vacatureNr))
{
this.Controls.Clear();
HtmlGenericControl table = new HtmlGenericControl("table");
HtmlGenericControl tr1 = new HtmlGenericControl("tr");
HtmlGenericControl td1 = new HtmlGenericControl("td");
HtmlGenericControl td2 = new HtmlGenericControl("td");
td1.InnerHtml = "<strong>Inzet in uren:</strong>";
td2.InnerHtml = "24-32";
tr1.Controls.Add(td1);
tr1.Controls.Add(td2);
table.Controls.Add(tr1);
HtmlGenericControl tr2 = new HtmlGenericControl("tr");
HtmlGenericControl td3 = new HtmlGenericControl("td");
HtmlGenericControl td4 = new HtmlGenericControl("td");
td3.InnerHtml = "<strong>Standplaats:</strong>";
td4.InnerHtml = "Egmond aan Zee";
tr2.Controls.Add(td3);
tr2.Controls.Add(td4);
table.Controls.Add(tr2);
HtmlGenericControl tr3 = new HtmlGenericControl("tr");
HtmlGenericControl td5 = new HtmlGenericControl("td");
HtmlGenericControl td6 = new HtmlGenericControl("td");
td5.InnerHtml = "<strong>Aard van dienstverband:</strong>";
td6.InnerHtml = "Bepaalde tijd";
tr3.Controls.Add(td5);
tr3.Controls.Add(td6);
table.Controls.Add(tr3);
this.Controls.Add(table);
}
}
The CreateChildControls
method will always be fired before the Page Load event. Instead, try moving your logic from CreateChildControls
into a new method and call this method at the end of Page_Load
.