我有一个二维(N×N)个C#字符串数组,我怎么得到它ouputted到网页动态(试图数据表/装订等) [英] 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...)

查看:237
本文介绍了我有一个二维(N×N)个C#字符串数组,我怎么得到它ouputted到网页动态(试图数据表/装订等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要输出到Web n页字符串数组一个n,我已经找到了一些解决方案,需要很多很多(阅读:许多)code线(通常是将其转换为一个DataTable,然后绑定到GridView)。而几乎所有这些解决方案甚至不会为我的阵列的动态性工作(我不知道提前多少行和列将要产生的,也不是列的名称:这是用户控制)

我觉得这是所有这些解决方案是有点荒谬的,在某些情况下比,我已经设计,简单地输出了一小块我的整个模块更大的我的数据...

这是什么,我试图做一个例子:

  object1 =(字符串[,])r.GetSymbol(字符串数组); //我检索的N×N个阵列,并投中所载的一个对象的字符串(有因为我使用的COM接口做到这一点)。
    Output_GridView.DataSource = object1; //(如果我尝试将此转换为字符串,则返回的dataType字符串不是二维数组
    Output_GridView.DataBind();

这不工作(它会根据错误我得到的,我不知道为什么数据源/ GridView控件将这样的限制需要一维数组),我已经在一些非常难看的解决方案,读了它,不过说真的,我需要的是只写一个嵌套的for循环输出n列n行到ASP.NET页面。有人可以帮助我在这里(为什么这样一个简单的任务一定要那么难吗?)

感谢您的任何反馈=)

戴夫


解决方案

你有没有考虑只创建一个自定义的Web控件。我创建了一个快速的其中一个接受一个数组[,],只是简单地用对绕每个数组值输出在一个div数组的内容。其简单,重量轻,你将有输出的完全控制。

下面是步骤需要实现:

一个新的项目添加到您的Web应用程序,并确保您所指的System.Web。也许调用项目器WebControls。

添加下面的C#code到一个新的类文件可以添加到项目中。

自定义控制code:

 使用System.ComponentModel;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;命名空间器WebControls
{
    [ToolboxData(< {0}:ArrayDisplayControl RUNAT =服务器>< / {0}:ArrayDisplayControl>中)
    公共类ArrayDisplayControl:WebControl的
    {
    保护覆盖的HtmlTextWriterTag TagKey
    {
        得到
        {
            返回HtmlTextWriterTag.Div;
        }
    }        公共字符串[,]数据源
        {
            得到
            {
                回报(字符串[,])的ViewState [数据源];
            }
            组
            {
                的ViewState [数据源] =值;
            }
        }        保护覆盖无效RenderContents(HtmlTextWriter的输出)
        {
            output.WriteBeginTag(分区);            的for(int i = 0; I< D​​ataSource.GetLength(0);我++)
            {
                为(中间体J = 0; J&下; DataSource.GetLength(1); J ++)
                {
                    output.WriteFullBeginTag(P);
                    output.Write(数据源[I,J]);
                    output.WriteEndTag(P);
                }
            }            output.WriteEndTag(分区);
        }
    }
}

现在你需要做的就是你的REF新增项目到Web应用程序。属性 - >添加引用 - 选择项目,然后将新项目的名称

好离开这就是一切是一个decleration添加到您asp.net页面的顶部,因此您可以参考类似下面的自定义控件:

 <%@注册大会=器WebControls命名空间=器WebControls标签preFIX =定制%GT;

现在引用控制你html格式如下所示:

 <定制:ArrayDisplayControl ID =ctlArrayDisplay=服务器/>

好了,所以最后一步是将控制在后面的code一些数据绑定 - 类似如下:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
            字符串[,]数据=新的字符串[2,2] {{迈克,艾米}​​,{玛丽,伟业}};            ctlArrayDisplay.DataSource =数据;
            ctlArrayDisplay.DataBind();
}

这里是输出后运行:

 < D​​IV ID =ctlArrayDisplay>
    <div><p>Mike</p><p>Amy</p><p>Mary</p><p>Albert</p></div>
&LT; / DIV&GT;

希望这有助于你出你的果酱。

享受!

I have an n by n String array that needs to be outputted to a web page, I have found some solutions that require many many (read: many) lines of code (usually converting it to a DataTable then binding to a GridView). And almost all of these solutions will not even work for the dynamic nature of my arrays (I do not know ahead of time how many columns and rows are going to be generated, nor the names of the columns: it's user controlled).

I find this to all of these solutions to be somewhat ridiculous and in some cases larger than my whole module that I have already designed, simply to output a small piece my data...

This is an example of what I have tried to do:

    object1 = (string[,]) r.GetSymbol("stringArray");  //I retrieve n x n array and cast as a string contained in an object (have to do this because I am using COM interfaces).
    Output_GridView.DataSource = object1;  //(If I try to convert this to a string, it returns the dataType "string" not the 2d array
    Output_GridView.DataBind();

This doesn't work (it would require a 1d array according to the error I get, I don't know why DataSource/GridView would be limited like this), I have read up on some very ugly solutions to it, but really, all I need is to just write a nested for-loop to output n columns and n rows to the ASP.NET page. Can someone help me out here (why does such a trivial task have to be so difficult?)

Thank you for any feedback =)

-Dave

解决方案

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!

这篇关于我有一个二维(N×N)个C#字符串数组,我怎么得到它ouputted到网页动态(试图数据表/装订等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆