1

I am trying to populate the contents of a List variable to a bootstrap modal. My problem is that the code only display the first element of the List variable.

This is my code:

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);

Additional code:

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 });
    }

Could someone highlight what I'm doing wrong and point me in the right direction?

Walters
  • 119
  • 16
  • You re opening up a lot of modals??? Does not make sense. – epascarello Apr 23 '19 at 17:04
  • 3
    You can only use RegisterStatupScript() once. That's your problem. – Jon Apr 23 '19 at 17:08
  • @Jon Okay Thanks for that. Did not know – Walters Apr 23 '19 at 17:09
  • @Jon It seem like that is the issue. I tested Rahul Sharma foreach idea and it still produced only the first element – Walters Apr 23 '19 at 17:12
  • 2
    Actually: you aren't limited from using RegisterStatupScript more than once, but you are limited from registering the same type/key combination more than once (this is a feature, not a limitation). (as pointed in https://stackoverflow.com/a/12534518/10577440) – Ronaldo Araújo Alves Apr 23 '19 at 17:14
  • But what suppose to happen is that the all the elements in the list would become concatenated into one single string variable, so wouldnt it need to be executed just once?? @RonaldoAraújoAlves – Walters Apr 23 '19 at 17:21
  • 1
    That is not what you are doing.... you are creating a new body on every iteration and writing out the script on every iteration. – epascarello Apr 23 '19 at 18:05

1 Answers1

0

SO I restructured the code logic and it worked. Instead of using multidimensional arrays, I just used a joined string. Workaround but it works perfect

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>
Walters
  • 119
  • 16