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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dynamic Table and Placeholder

Dynamic Table and Placeholder

Scheduled Pinned Locked Moved ASP.NET
html
4 Posts 3 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.
  • E Offline
    E Offline
    econner
    wrote on last edited by
    #1

    Hello, I am looking for a way to dynamically create a table with different cells and then dynamically create Placeholder controls into the table cells based on my table layout. I know that you can create a table on the form and then put a placeholder in the cell (which creates an instance of () in the html code, but I am looking for a way to create this dynamically from with the page_load or OnInit methods. I want to control the number of placeholders and their location dynamically.

    T 1 Reply Last reply
    0
    • E econner

      Hello, I am looking for a way to dynamically create a table with different cells and then dynamically create Placeholder controls into the table cells based on my table layout. I know that you can create a table on the form and then put a placeholder in the cell (which creates an instance of () in the html code, but I am looking for a way to create this dynamically from with the page_load or OnInit methods. I want to control the number of placeholders and their location dynamically.

      T Offline
      T Offline
      Torsten Mauz
      wrote on last edited by
      #2

      I'm think I understand the question...not sure though, but anyway i've coded up a swift demo of what I think you're after. You will need some kind of logic to define where the placeholders will go but this will give you a start:

      Note: This is the codebehind for a web form which had a table in it called tblLayout (<asp:table id="tblLayout")
      public class test : System.Web.UI.Page { protected System.Web.UI.WebControls.Table tblLayout; protected PlaceHolder ph1; protected PlaceHolder ph2; protected PlaceHolder ph3; protected PlaceHolder ph4; private void Page_Load(object sender, System.EventArgs e) { ph1 = new PlaceHolder(); ph2 = new PlaceHolder(); ph3 = new PlaceHolder(); ph4 = new PlaceHolder(); for(int i = 0; i < 5; i++) { TableRow tr = new TableRow(); for(int x = 0; x < 3; x++) { TableCell td = new TableCell(); if(x == 0 && i == 0) td.Controls.Add(ph1); if(x == 1 && i == 1) td.Controls.Add(ph2); if(x == 2 && i == 2) td.Controls.Add(ph3); if(x == 0 && i == 4) td.Controls.Add(ph4); tr.Cells.Add(td); } this.tblLayout.Rows.Add(tr); } Button b1 = new Button(); b1.Text = "Button 1"; ph1.Controls.Add(b1); TextBox t1 = new TextBox(); t1.Text = "TextBox 1"; ph2.Controls.Add(t1); Label l1 = new Label(); l1.Text = "Label 1"; ph3.Controls.Add(l1); CheckBox c1 = new CheckBox(); c1.Text = "CheckBox 1"; ph4.Controls.Add(c1); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion }

      This is how you programatically add controls, cells and rows to a table control. Although to be honest, i'm not sure why you want to add the placeholders dynamically. The idea behind using placeholders is to provide a container for child controls. In other words, you would add the placeholders in the html, and use them to programatically add other controls in specific locations. If the above example is of no use, perhaps you could explain what you are trying to achieve then we can try to offer some more accurate sol

      N 1 Reply Last reply
      0
      • T Torsten Mauz

        I'm think I understand the question...not sure though, but anyway i've coded up a swift demo of what I think you're after. You will need some kind of logic to define where the placeholders will go but this will give you a start:

        Note: This is the codebehind for a web form which had a table in it called tblLayout (<asp:table id="tblLayout")
        public class test : System.Web.UI.Page { protected System.Web.UI.WebControls.Table tblLayout; protected PlaceHolder ph1; protected PlaceHolder ph2; protected PlaceHolder ph3; protected PlaceHolder ph4; private void Page_Load(object sender, System.EventArgs e) { ph1 = new PlaceHolder(); ph2 = new PlaceHolder(); ph3 = new PlaceHolder(); ph4 = new PlaceHolder(); for(int i = 0; i < 5; i++) { TableRow tr = new TableRow(); for(int x = 0; x < 3; x++) { TableCell td = new TableCell(); if(x == 0 && i == 0) td.Controls.Add(ph1); if(x == 1 && i == 1) td.Controls.Add(ph2); if(x == 2 && i == 2) td.Controls.Add(ph3); if(x == 0 && i == 4) td.Controls.Add(ph4); tr.Cells.Add(td); } this.tblLayout.Rows.Add(tr); } Button b1 = new Button(); b1.Text = "Button 1"; ph1.Controls.Add(b1); TextBox t1 = new TextBox(); t1.Text = "TextBox 1"; ph2.Controls.Add(t1); Label l1 = new Label(); l1.Text = "Label 1"; ph3.Controls.Add(l1); CheckBox c1 = new CheckBox(); c1.Text = "CheckBox 1"; ph4.Controls.Add(c1); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion }

        This is how you programatically add controls, cells and rows to a table control. Although to be honest, i'm not sure why you want to add the placeholders dynamically. The idea behind using placeholders is to provide a container for child controls. In other words, you would add the placeholders in the html, and use them to programatically add other controls in specific locations. If the above example is of no use, perhaps you could explain what you are trying to achieve then we can try to offer some more accurate sol

        N Offline
        N Offline
        neha12
        wrote on last edited by
        #3

        Thanks to who ever u are... Ur explanation is very specific.. Hope everyone answers in the same way.. Thanks a lot...

        T 1 Reply Last reply
        0
        • N neha12

          Thanks to who ever u are... Ur explanation is very specific.. Hope everyone answers in the same way.. Thanks a lot...

          T Offline
          T Offline
          Torsten Mauz
          wrote on last edited by
          #4

          My pleasure, glad to be of help.

          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