Moving page controls to a form and then the form back to the page?
-
Hello, first note that this code concerns .net framework 1.1. I'm having trouble with the following: I have a class derived from Page. In the Page_Load of this class I'd like to do the following:
HtmlForm form = new HtmlForm();
form.Name = "test";
form.ID = form.Name;
form.Controls.Add(_header); // user controls I've instanciated and loaded
using LoadControl(...)
form.Controls.Add(_menu);
...// add child controls from the derived page
for(int i = 0; i < Page.Controls.Count; i++)
{
form.Controls.Add(Page.Controls[i]);
}form.Controls.Add(_footer);
Page.Controls.Clear();
Page.Controls.Add(form);What I'd like to achive is to have a base page with a couple of controls on, from which i can derive further. The problem I'm having is, that on each round in the for loop, when I "read" the Page.Control at the given index, the Page.Controls.Count decreases by one. Could somebody please shed some light. Thanks in advance! Greetings from Berlin, Matthias
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
Hello, first note that this code concerns .net framework 1.1. I'm having trouble with the following: I have a class derived from Page. In the Page_Load of this class I'd like to do the following:
HtmlForm form = new HtmlForm();
form.Name = "test";
form.ID = form.Name;
form.Controls.Add(_header); // user controls I've instanciated and loaded
using LoadControl(...)
form.Controls.Add(_menu);
...// add child controls from the derived page
for(int i = 0; i < Page.Controls.Count; i++)
{
form.Controls.Add(Page.Controls[i]);
}form.Controls.Add(_footer);
Page.Controls.Clear();
Page.Controls.Add(form);What I'd like to achive is to have a base page with a couple of controls on, from which i can derive further. The problem I'm having is, that on each round in the for loop, when I "read" the Page.Control at the given index, the Page.Controls.Count decreases by one. Could somebody please shed some light. Thanks in advance! Greetings from Berlin, Matthias
/matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]matthias s. wrote:
The problem I'm having is, that on each round in the for loop, when I "read" the Page.Control at the given index, the Page.Controls.Count decreases by one.
:confused: Hearing such a problem first time. Try assigning the page count to a variable before starting the loop. ? Seeing your base class design, I got confused. Why you need add the header like this ? if you know header should be there in all pages, why don't you add this in the design time itself ? [edit] One more point, I don't think ASP.NET can maintain
viewstate
for the dynamic controls added on page_load, because viewstate would be already loaded beforepage_load
fires. [/edit]