I’d like to programmatically build an HtmlTable/Table in the code behind of a webservice and have it return as a string of HTML so that it can be written with JavaScript to the innerhtml of a span/div/whatever
Something similar to the following:
//webservice.amsx.cs Build the table, called by another/different method
protected string Table_Maker()
{
HtmlTable tbl = new HtmlTable();
HtmlTableCell cell = new HtmlTableCell();
HtmlTableRow row = new HtmlTableRow();
cell.InnerText = "WhateverText";
row.Cells.Add(cell);
tbl.Rows.Add(row);
return tbl.ToString();
}
//somepage.aspx write table to the div
function menuHelper(toplayer, toplayernumber)
{
var sublayertable;
var sublayerpostcompileID;
var toplayernumber;
menuHelper_Part1();
function menuHelper_Part1()
{
//replace ucMainMenu with ucMainMenu_pnlContent
sublayerpostcompileID = toplayer.replace("ucMainMenu", "ucMainMenu_pnlContent");
//Call the webmethod
webbernetz.MenuHelperWebService.Sub_Menu_Helper(toplayernumber, menuHelper_Part2);
}
function menuHelper_Part2(result){
//Write the result to the target area
document.getElementById(sublayerpostcompileID).innerHTML = result;
}
}
When I return it to the javascript the javascript simply writes “System.Web.UI.HtmlControls.HtmlTable”.
How di I get it to write the actual table?
The only issue with your code is the use of
tbl.ToString().As noted in some of the other posts, you should
Renderthe table control using anHtmlTextWriterto aStringBuilderobject, which can then return a string value for your method. Something like this:That should return the HTML to be inserted into your page.