如何显示在 .aspx 网站中以编程方式生成的 table

How to display a table which is generated programmatically in a .aspx-Website

我在显示以编程方式生成的 table 时遇到问题。让我解释。 我有一个 XML 看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<Fragebogen>
  <Header>Header</Header>
  <Tables>
    <Table>
      <Rows>
        <Row>
          <Cells>
            <Cell>Cell 1 Row 1</Cell>
          </Cells>
        </Row>
        <Row>
          <Cells>
            <Cell>Cell 1 Row 2</Cell>
          </Cells>
        </Row>
        <Row>
          <Cells>
            <Cell>Cell 1 Row 3</Cell>
            <Cell>Cell 2 Row 3</Cell>
            <Cell>Cell 3 Row 3</Cell>
          </Cells>
        </Row>
      </Rows>
    </Table>
  </Tables>
</Fragebogen>

这个方法:

if (String.IsNullOrWhiteSpace(file) && node == null) return null;

// If a file is not empty then load the xml and overwrite node with the
// root element of the loaded document
node = !String.IsNullOrWhiteSpace(file) ? XDocument.Load(file).Root : node;

IDictionary<String, dynamic> result = new ExpandoObject();

// implement fix as suggested by [ndinges]
var pluralizationService =
    PluralizationService.CreateService(CultureInfo.CreateSpecificCulture("en-us"));

// use parallel as we dont really care of the order of our properties
node.Elements().AsParallel().ForAll(gn =>
{
    // Determine if node is a collection container
    var isCollection = gn.HasElements &&
        (
        // if multiple child elements and all the node names are the same
            gn.Elements().Count() > 1 &&
            gn.Elements().All(
                e => e.Name.LocalName.ToLower() == gn.Elements().First().Name.LocalName) ||

            // if there's only one child element then determine using the PluralizationService if
        // the pluralization of the child elements name matches the parent node. 
            gn.Name.LocalName.ToLower() == pluralizationService.Pluralize(
                gn.Elements().First().Name.LocalName).ToLower()
        );

    // If the current node is a container node then we want to skip adding
    // the container node itself, but instead we load the children elements
    // of the current node. If the current node has child elements then load
    // those child elements recursively
    var items = isCollection ? gn.Elements().ToList() : new List<XElement>() { gn };

    var values = new List<dynamic>();

    // use parallel as we dont really care of the order of our properties
    // and it will help processing larger XMLs
    items.AsParallel().ForAll(i => values.Add((i.HasElements) ?
       GetExpandoFromXml(null, i) : i.Value.Trim()));

    // Add the object name + value or value collection to the dictionary
    result[gn.Name.LocalName] = isCollection ? values : values.FirstOrDefault();
});
return result;

正在将节点放入 Expando-Object。对于这个对象,我想以编程方式在 .aspx 网站中生成 table。为了生成我调用这个方法:

dynamic fragebogen = GetExpandoFromXml(SSG.Fragebogen.Properties.Settings.Default.ConfigPath);
var header = fragebogen.Header;
var tables = fragebogen.Tables;
Table t;
var rows = tables[0].Rows;
TableRow r;
dynamic cells = null;
for (int i = 0; i < rows.Count; i++)
{
    cells = rows[i].Cells;
}
TableCell c;

foreach (var table in tables)
{
    t = new Table();
    foreach (var row in rows)
    {
        r = new TableRow();
        t.Rows.Add(r);
        foreach (var cell in cells)
        {
            c = new TableCell();
            c.Text = cell;
            r.Cells.Add(c);
        }
    }
}

到目前为止一切都很好。行和单元格都添加到具有正确值的 table 并且没有异常或其他任何内容, 但是 table 未显示在网站。 真不知道为什么没有显示,希望大家能帮帮我。

要显示此动态生成的 Table,您还需要将其添加到页面的某处。在您的页面上放置一个占位符

<asp:PlaceHolder runat="server" ID="phOnme"></asp:PlaceHolder>

然后在后面的代码中将您的 table 放置到该持有人处:

Table t;
// generate the table...
// add it to page.
phOnme.Controls.Add(t);