0

I've been stuck for the last two hours and need help. I've tried every single example I could find online and none of them work. I'm building a web page in ASP.NET and want to make a list of buttons. Doesn't seem too hard right? It's been causing me issues though.

This is my code:

<ul>
  <form id="tagloop" runat="server">
    <% foreach (string i in data)%>
      <%Response.Write("<li><button runat=\"server\" type=\"submit\" onserverclick=\"ClickTag\">" + i + "</button></li>");%>
  </form>
</ul>

If I remove the Response.Write() it only loops once but the one button it does generate actually works and calls the method on click. Also, the variable i doesn't apply.

<ul>
  <form id="tagloop" runat="server">
    <% foreach (string i in data)%>
      <li><button runat="server" type="submit" onserverclick="ClickTag"> i </button></li>
  </form>
</ul>

Is there anyway I can get it to loop, have the text of i in data, and also call the correct function on click? I haven't been able to find a balance of all three yet and any help will be appreciated.

Brian
  • 5,069
  • 7
  • 37
  • 47
  • First add {} around the
  • , I didn’t think this would even work without it. You should never really use Response.Write and it definitely doesn’t work with server-side controls as you noticed.
  • – Sami Kuhmonen Jun 08 '18 at 18:31
  • What's your preferred solution. Do you want server side controls with code behind event handlers? – Marco Jun 08 '18 at 18:43
  • It possibly loops once because you have only 1 object in data? – Aman Jun 08 '18 at 18:46