I have a 2d (n x n) string array in C#, how do I get it ouputted to a webpage dynamically (Tried DataTables/Binding, etc…)

南笙酒味 提交于 2019-12-02 05:57:58

Have you considered just creating a custom web control. I have created a quick one which accepts an array [,] and just simply outputs the contents of the array in a div with p's around each array value. Its simple, lightweight and you will have complete control of the output.

Here are the step you need to implement:

Add a new project to your web application and make sure you referent System.web. Maybe call the project WebControls.

Add the following C# code to a new class file you can add to the project.

CUSTOM CONTROL CODE:

using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebControls
{
    [ToolboxData("<{0}:ArrayDisplayControl runat=server></{0}:ArrayDisplayControl>")]
    public class ArrayDisplayControl : WebControl
    {
    protected override HtmlTextWriterTag TagKey
    {
        get
        {
            return HtmlTextWriterTag.Div;
        }
    }

        public string[,] DataSource
        {
            get
            {
                return (string[,])ViewState["DataSource"];
            }
            set
            {
                ViewState["DataSource"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.WriteBeginTag("div");

            for (int i = 0; i < DataSource.GetLength(0); i++)
            {
                for (int j = 0; j < DataSource.GetLength(1); j++)
                {
                    output.WriteFullBeginTag("p");
                    output.Write(DataSource[i, j]);
                    output.WriteEndTag("p");
                }
            }

            output.WriteEndTag("div");
        }
    }
}

Now all you have to do is ref your newly added project to your web application. properties --> add reference - select projects and then the name of the new project.

Ok all thats left is to add a decleration to the top of you asp.net page so you can reference the custom control like the following:

<%@ Register Assembly="WebControls" Namespace="WebControls" TagPrefix="custom" %>

Now reference the control in you html like the following:

<custom:ArrayDisplayControl ID="ctlArrayDisplay" runat="server" />

Ok so last step is to bind the control to some data in the code behind - something like the following:

protected void Page_Load(object sender, EventArgs e)
{
            string[,] data = new string[2, 2] { { "Mike", "Amy" }, { "Mary", "Albert" } };

            ctlArrayDisplay.DataSource = data;
            ctlArrayDisplay.DataBind();
}

And here is the output after running:

<div id="ctlArrayDisplay"> 
    <div><p>Mike</p><p>Amy</p><p>Mary</p><p>Albert</p></div> 
</div> 

Hope this help you out of your jam.

Enjoy!

not pretty but should give you a start

    //setup the data
    var random = new Random();
    int x = random.Next(14) + 1;
    int y = random.Next(29) + 1;

    var data = new int[x, y];
    for (int i = 0; i < x; i++)
        for (int j = 0; j < y; j++)
            data[i, j] = random.Next(100);


    //create the data table
    var table = new Table();
    for (int i = 0; i < x; i++)
    {
        var newRow = new TableRow();
        for (int j = 0; j < y; j++)
        {
            var newCell = new TableCell();
            newCell.Text = data[i,j].ToString();
            newRow.Cells.Add(newCell);
        }
        table.Rows.Add(newRow);
    }
    ux_Panel.Controls.Add(table);

Shlomo's looks like the better solution unless you want to massage the data a bit more

Although ugly, ASP Code Nuggets in the .aspx would work:

<table>
  <% for(int i = 0; i < ArrayDimensionLength; i++) { %>
    <tr>
      <td>RowHeader</td>
      <% for(int j = 0; j < ArrayDimensionLength; j++) { %>
        <td><%= Array[i,j] %> </td>  
      <% } %>
    </tr>
  <% } %>
</table>

...and in the code-behind:

    protected string[,] MyArray
    {
        get
        {
            //Insert your array here
            return new string[,]{{"1", "1"}, {"2", "2"}};
        }
    }

    protected int ArrayDimensionLength
    {
        get
        {
            return (int)Math.Sqrt(MyArray.Length);
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!