ASP.NET Page Inheritance and Overriding onInit
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
var template = LoadControl(Path);
if (template == null)
throw new Exception("Could not located the Template");
var templateControl = template.FindControl("_placeHolder");
if (templateControl == null)
throw new Exception("Could not located the Place Holder");
templateControl.Controls.Add(_container);
//This doesnt Work
for (var i = 0; i < template.Controls.Count;i++ )
{
va开发者_开发问答r myControl = template.Controls[i];
Controls.Add(myControl);
}
//This works
for (var i = 0; i < template.Controls.Count; i++)
{
var myControl = template.Controls[0];
template.Controls.Remove(myControl);
Controls.Add(myControl);
}
}
This is just a Tutorial I am working on. The question is Why does the Second for loop display the output on the page and the first one does not. I do comment out one of them to test this. What exactly am i missing and why do i need to remove the control?
What I see from your code is
for (var i = 0; i < template.Controls.Count; i++)
{
var myControl = template.Controls[0];
template.Controls.Remove(myControl);
Controls.Add(myControl);
}
This loop (Second one) doesn't really have any use. It just removes the same control each time, as there is no use of "i". Every time it removes Controls[0], and re-add it to the Controls.
Hope it helps. Don't forget to upvote it if it solves you problem. Thanks.. :)
精彩评论