Lax
Lax

Reputation: 113

How to create IFrame dynamically in runtime in loop in MVC

I have a view in MVC in which iframe needs to be created on runtime inside a for loop. Each iframe has a different source. Please can anyone suggest a solution with sample code. Thanxx I tried the below code but its not working..

<div class="width_100" id="div3">

     @{  for (int i = 0; i < datatable1.Rows.Count; i++)
      {

          <script type="text/javascript">
              window.onload = function () {
                  var iframe = document.createElement('iframe');
                  iframe.frameBorder = 0;
                  iframe.width = "300px";
                  iframe.height = "250px";
                  iframe.id = "randomid";
                  iframe.setAttribute("@datatable1.Rows[i]["UploadTitle"].ToString()", "");
                  document.getElementById("div3").appendChild(iframe);
               }
          </script>

      }
      }

  }

</div>

Upvotes: 2

Views: 813

Answers (1)

SLaks
SLaks

Reputation: 887837

You don't need Javascript at all.

Instead, you can write regular <iframe> tags in your HTML inside the loop.

Upvotes: 1

Related Questions