C#多维数组仅打印第一个元素

时间:2019-04-23 17:00:42

标签: javascript c# asp.net asp.net-mvc webforms

我正在尝试将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 });
    }

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

1 个答案:

答案 0 :(得分:0)

因此,我重新构建了代码逻辑,它开始工作了。我没有使用多维数组,而是使用了连接字符串。解决方法,但效果很好

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>