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. Code behind for ASP tables

Code behind for ASP tables

Scheduled Pinned Locked Moved ASP.NET
htmltutorialquestion
13 Posts 5 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 Declan Bright

    Use a StringBuilder to build the string of HTML and then set the string as the Text of a Literal control.

    Declan Bright www.declanbright.com

    W Offline
    W Offline
    wbeetge
    wrote on last edited by
    #3

    Thanks, sounds easy. I need to load other controls (images, text, hyperlinks etc) into different cells of the table. Can I talk to the cells in the table after setting the string of a literal control or must I build the string with the controls embedded in the string for each record in the query result ?

    A 1 Reply Last reply
    0
    • W wbeetge

      Thanks, sounds easy. I need to load other controls (images, text, hyperlinks etc) into different cells of the table. Can I talk to the cells in the table after setting the string of a literal control or must I build the string with the controls embedded in the string for each record in the query result ?

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #4

      why you are not creating table runtime using Table Class?

      Best Regards ----------------- Abhijit Jana Microsoft Certified Professional "Success is Journey it's not a destination"

      W 1 Reply Last reply
      0
      • W wbeetge

        Can anyone please show me how to create the following table (given in markup) in code behind ? <div style="text-align: left"> <table border="0" cellpadding="1" style="position: relative"> <tr> <td rowspan="4" style="width: 100px"> </td> <td style="width: 100px"> </td> </tr> <tr> <td style="width: 100px"> </td> </tr> <tr> <td style="width: 100px"> </td> </tr> <tr> <td style="width: 100px"> </td> </tr> </table> </div> As you can see, the cells in the left column are merged. Thanks WBeetge

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #5

        You do this by building up the control tree using a Panel for the div and a Table, TableRow's and TableCell's for the table. I have simplified this somewhat (mainly creating a simple method that generates the 100px cell and reusing that throughout) for the purpose of demonstration but here you go: protected override void OnLoad(EventArgs e) { Panel panel = new Panel(); panel.Style.Add(HtmlTextWriterStyle.TextAlign, "left"); Table table = new Table(); table.CellPadding = 1; table.Style.Add(HtmlTextWriterStyle.Position, "relative"); panel.Controls.Add(table); TableRow row1 = new TableRow(); TableCell colRowSpan = this.Create100PixelCell("a"); colRowSpan.RowSpan = 4; row1.Cells.Add(colRowSpan); row1.Cells.Add(this.Create100PixelCell("b")); table.Rows.Add(row1); TableRow row2 = new TableRow(); row2.Cells.Add(this.Create100PixelCell("c")); table.Rows.Add(row2); TableRow row3 = new TableRow(); row3.Cells.Add(this.Create100PixelCell("d")); table.Rows.Add(row3); TableRow row4 = new TableRow(); row4.Cells.Add(this.Create100PixelCell("e")); table.Rows.Add(row4); this.placeHolder.Controls.Add(panel); } private TableCell Create100PixelCell(string someContent) { TableCell cell = new TableCell(); cell.Style.Add(HtmlTextWriterStyle.Width, "100px"); cell.Controls.Add(new LiteralControl(someContent)); return cell; } The line "this.placeHolder..." is an asp:PlaceHolder put into the markup where i want the dynamic content to be placed. hope it helps.

        W A 2 Replies Last reply
        0
        • A Abhijit Jana

          why you are not creating table runtime using Table Class?

          Best Regards ----------------- Abhijit Jana Microsoft Certified Professional "Success is Journey it's not a destination"

          W Offline
          W Offline
          wbeetge
          wrote on last edited by
          #6

          If anyone can show me how to do that for the example table (left Column cells are merged) using the table, tablerow and tablecell objects, or any other method that will work, I will use that method.

          J 1 Reply Last reply
          0
          • W wbeetge

            If anyone can show me how to do that for the example table (left Column cells are merged) using the table, tablerow and tablecell objects, or any other method that will work, I will use that method.

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #7

            see my post below.

            1 Reply Last reply
            0
            • J J4amieC

              You do this by building up the control tree using a Panel for the div and a Table, TableRow's and TableCell's for the table. I have simplified this somewhat (mainly creating a simple method that generates the 100px cell and reusing that throughout) for the purpose of demonstration but here you go: protected override void OnLoad(EventArgs e) { Panel panel = new Panel(); panel.Style.Add(HtmlTextWriterStyle.TextAlign, "left"); Table table = new Table(); table.CellPadding = 1; table.Style.Add(HtmlTextWriterStyle.Position, "relative"); panel.Controls.Add(table); TableRow row1 = new TableRow(); TableCell colRowSpan = this.Create100PixelCell("a"); colRowSpan.RowSpan = 4; row1.Cells.Add(colRowSpan); row1.Cells.Add(this.Create100PixelCell("b")); table.Rows.Add(row1); TableRow row2 = new TableRow(); row2.Cells.Add(this.Create100PixelCell("c")); table.Rows.Add(row2); TableRow row3 = new TableRow(); row3.Cells.Add(this.Create100PixelCell("d")); table.Rows.Add(row3); TableRow row4 = new TableRow(); row4.Cells.Add(this.Create100PixelCell("e")); table.Rows.Add(row4); this.placeHolder.Controls.Add(panel); } private TableCell Create100PixelCell(string someContent) { TableCell cell = new TableCell(); cell.Style.Add(HtmlTextWriterStyle.Width, "100px"); cell.Controls.Add(new LiteralControl(someContent)); return cell; } The line "this.placeHolder..." is an asp:PlaceHolder put into the markup where i want the dynamic content to be placed. hope it helps.

              W Offline
              W Offline
              wbeetge
              wrote on last edited by
              #8

              Thanks J4amieC That is exactly what I am looking for.

              J 1 Reply Last reply
              0
              • W wbeetge

                Thanks J4amieC That is exactly what I am looking for.

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #9

                you're very welcome. Remember if you want events from any of these dynamically created controls you need to re-create them after postback. It gets tricky quickly.

                W 1 Reply Last reply
                0
                • W wbeetge

                  Can anyone please show me how to create the following table (given in markup) in code behind ? <div style="text-align: left"> <table border="0" cellpadding="1" style="position: relative"> <tr> <td rowspan="4" style="width: 100px"> </td> <td style="width: 100px"> </td> </tr> <tr> <td style="width: 100px"> </td> </tr> <tr> <td style="width: 100px"> </td> </tr> <tr> <td style="width: 100px"> </td> </tr> </table> </div> As you can see, the cells in the left column are merged. Thanks WBeetge

                  S Offline
                  S Offline
                  Sneaki
                  wrote on last edited by
                  #10

                  Well the problem with merging is that due to the fact that the cells has nothing in it the table just ignores it, therefore best way to prevent that is putting something in it like a label, textbox, etc. But if you would like to leave it blank then best option is insert a new line statement "
                  ".

                  Then you could try to do it in the code-behind with the feature of Response.Write("...html coding...."), i tried it with a picture e.g. This is adding a picture with a string from the database to a checkListBox collection cblNames.Items.Add((string)readerSet["Nam"] + "

                  "); Any other questions be free to ask, i'll try my best...

                  W 1 Reply Last reply
                  0
                  • J J4amieC

                    You do this by building up the control tree using a Panel for the div and a Table, TableRow's and TableCell's for the table. I have simplified this somewhat (mainly creating a simple method that generates the 100px cell and reusing that throughout) for the purpose of demonstration but here you go: protected override void OnLoad(EventArgs e) { Panel panel = new Panel(); panel.Style.Add(HtmlTextWriterStyle.TextAlign, "left"); Table table = new Table(); table.CellPadding = 1; table.Style.Add(HtmlTextWriterStyle.Position, "relative"); panel.Controls.Add(table); TableRow row1 = new TableRow(); TableCell colRowSpan = this.Create100PixelCell("a"); colRowSpan.RowSpan = 4; row1.Cells.Add(colRowSpan); row1.Cells.Add(this.Create100PixelCell("b")); table.Rows.Add(row1); TableRow row2 = new TableRow(); row2.Cells.Add(this.Create100PixelCell("c")); table.Rows.Add(row2); TableRow row3 = new TableRow(); row3.Cells.Add(this.Create100PixelCell("d")); table.Rows.Add(row3); TableRow row4 = new TableRow(); row4.Cells.Add(this.Create100PixelCell("e")); table.Rows.Add(row4); this.placeHolder.Controls.Add(panel); } private TableCell Create100PixelCell(string someContent) { TableCell cell = new TableCell(); cell.Style.Add(HtmlTextWriterStyle.Width, "100px"); cell.Controls.Add(new LiteralControl(someContent)); return cell; } The line "this.placeHolder..." is an asp:PlaceHolder put into the markup where i want the dynamic content to be placed. hope it helps.

                    A Offline
                    A Offline
                    Abhijit Jana
                    wrote on last edited by
                    #11

                    Greate Solluation !!!!! Do you have any answer of my last Question that i have post regarding TextBox? Any kind of help !!!

                    Best Regards ----------------- Abhijit Jana Microsoft Certified Professional "Success is Journey it's not a destination"

                    1 Reply Last reply
                    0
                    • J J4amieC

                      you're very welcome. Remember if you want events from any of these dynamically created controls you need to re-create them after postback. It gets tricky quickly.

                      W Offline
                      W Offline
                      wbeetge
                      wrote on last edited by
                      #12

                      Yes, I tried this by creating the table in a Web User control, and found that same problem. But though it worked in Dev, I could not build the site with the control - all kinds of errors. So, now I have to resort to a basic dynamic creation of this table.

                      1 Reply Last reply
                      0
                      • S Sneaki

                        Well the problem with merging is that due to the fact that the cells has nothing in it the table just ignores it, therefore best way to prevent that is putting something in it like a label, textbox, etc. But if you would like to leave it blank then best option is insert a new line statement "
                        ".

                        Then you could try to do it in the code-behind with the feature of Response.Write("...html coding...."), i tried it with a picture e.g. This is adding a picture with a string from the database to a checkListBox collection cblNames.Items.Add((string)readerSet["Nam"] + "

                        "); Any other questions be free to ask, i'll try my best...

                        W Offline
                        W Offline
                        wbeetge
                        wrote on last edited by
                        #13

                        Thanks everyone. I can continue with your suggestions

                        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