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. How to get Server control to access parent container's controls collection?

How to get Server control to access parent container's controls collection?

Scheduled Pinned Locked Moved ASP.NET
sysadmindockerhelptutorialquestion
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.
  • D Offline
    D Offline
    Dominic Pettifer
    wrote on last edited by
    #1

    Hello. Is there any way a custom server control can can programmatically add controls to the parent pages controls collection? I've tried the following... protected override void Render(HtmlTextWriter writer) {    this.Page.Controls.Add(new HtmlGenericControl("div"));    .... } ...but it gives me an error "Collection was modified; enumeration operation may not execute.". I've also tried adding the above code into the OnPreRender and OnInit events but it still gives me an error, this time "The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.". What I am trying to do is get a server control to add a div tag to the parent page but positioned just after the opening body tag or just before the closing body tag, just so that it's not wrapped in any other elements basically. Something tells me this isn't possible but thought I'd ask you folks before I gave up on this.

    Dominic Pettifer Blog: www.dominicpettifer.co.uk

    K 1 Reply Last reply
    0
    • D Dominic Pettifer

      Hello. Is there any way a custom server control can can programmatically add controls to the parent pages controls collection? I've tried the following... protected override void Render(HtmlTextWriter writer) {    this.Page.Controls.Add(new HtmlGenericControl("div"));    .... } ...but it gives me an error "Collection was modified; enumeration operation may not execute.". I've also tried adding the above code into the OnPreRender and OnInit events but it still gives me an error, this time "The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.". What I am trying to do is get a server control to add a div tag to the parent page but positioned just after the opening body tag or just before the closing body tag, just so that it's not wrapped in any other elements basically. Something tells me this isn't possible but thought I'd ask you folks before I gave up on this.

      Dominic Pettifer Blog: www.dominicpettifer.co.uk

      K Offline
      K Offline
      koleraba
      wrote on last edited by
      #2

      Usualy the reason for this error is that the collection is changed(items added or removed) within the loop.

      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