Code behind for ASP tables
-
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
-
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
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
-
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
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 ?
-
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 ?
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"
-
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
You do this by building up the control tree using a
Panel
for the div and aTable
,TableRow
's andTableCell
'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. -
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"
-
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.
-
You do this by building up the control tree using a
Panel
for the div and aTable
,TableRow
's andTableCell
'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. -
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
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... -
You do this by building up the control tree using a
Panel
for the div and aTable
,TableRow
's andTableCell
'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.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"
-
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.
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.
-
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...