Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Moving page controls to a form and then the form back to the page?

Moving page controls to a form and then the form back to the page?

Scheduled Pinned Locked Moved ASP.NET
csharpdatabasewinformshelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    matthias s 0
    wrote on last edited by
    #1

    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]

    N 1 Reply Last reply
    0
    • M matthias s 0

      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]

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      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 before page_load fires. [/edit]


      My Website | Ask smart questions

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups