首页 > 解决方案 > C#多维数组只打印第一个元素

问题描述

我正在尝试将List变量的内容填充到引导模式。我的问题是代码只显示List变量的第一个元素。

这是我的代码:

List<string[]> listStatus = new List<string[]>();
 for (int i = 0; i < listStatus.Count; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                string body = listStatus[i][j+1].ToString();
                body = body + "  - " + listStatus[i][j].ToString();
                ClientScript.RegisterStartupScript(this.GetType(), "Popup", "populateModal('" + body + "');", true);
            }
        }
        ClientScript.RegisterStartupScript(this.GetType(), "Show", "showModal()", true);

附加代码:

protected void btnSplunk_Click(object sender, EventArgs e)
    {
        string sp_ip = "172.16.159.94";
        int sp_port = 22;
        string status = CheckCon(sp_ip, sp_port, lblSplunkUpdate);
        listStatus.Add(new string[] { status, sp_ip });
    }

    protected void btnRandom_Click(object sender, EventArgs e)
    {
        string ran_ip = "134.32.233.21";
        int ran_port = 801;
        string status = CheckCon(ran_ip, ran_port, lblRnadomUpdate);
        listStatus.Add(new string[] { status, ran_ip });
    }

有人可以强调我做错了什么并指出我正确的方向吗?

标签: javascriptc#asp.netasp.net-mvcwebforms

解决方案


所以我重组了代码逻辑并且它起作用了。我没有使用多维数组,而是使用了一个连接字符串。解决方法,但效果很好

List<string> listStatus = new List<string>();
string body = "";
        for (int i = 0; i < listStatus.Count; i++)
        {
            string var = listStatus[i].ToString();
            body = body + var + "</br>";
        }
        Response.Write(body + "</br>");
        ClientScript.RegisterStartupScript(this.GetType(), "Popup", "openModal('" + body + "');", true);
        ClientScript.RegisterStartupScript(this.GetType(), "Show", "showModal()", true);

JavaScript:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<script type="text/javascript">
    function openModal(body) {
        $("#exampleModalCenter .modal-body").html(body);
    }
    function showModal() {
        $("#exampleModalCenter").modal("show");
    }
</script>

推荐阅读