导致问题的复杂字符串的串联

Concatenation of a Complicated String Causing Issues

我已经将一段代码以正确的格式分配给一个字符串。检查它工作正常。现在我希望在按钮 click.Also 上获得与旧代码串联的这段代码的副本,字符串中有变量从文本框中获取值。但不知何故,这些变量(数组)无法存储最后的值。我想生成代码,同时将所有变量放在适当的位置。

  public string oldCode;
    public int counter=0;
    string[] AffUrl = new string[100];
    string[] ImgUrl = new string[100];
    private void button1_Click(object sender, EventArgs e)
    {

        AffUrl[counter] =textBox1.Text;
        ImgUrl[counter] = textBox2.Text;


        string code = @"<div style=""float:left;padding-right:30px;padding-bottom:30px;"">
<div>
  <img src=""" + ImgUrl[counter] + @""" height=""200px"" width=""200px"" />
</div>
<div>
  <button onclick=""myFunction()"">Try it</button>
  <script type=""text/javascript"">
    function myFunction" + counter + @"() {
      var btn = document.createElement(""BUTTON"");
      window.open(""" + AffUrl[counter] + @""", ""_self"")
    }
  </script>
</div>
  </div>";
       oldCode = code;
       oldCode = string.Concat(code,oldCode);
       counter++;

       richTextBox1.Text =oldCode; 
    }

正如所写,您总是将新值与其自身连接起来。删除 oldCode = code 分配。

  </div>";
   oldCode = code; <= here you reset the cache - are you sure that's intended?
   oldCode = string.Concat(code,oldCode);

好的,我知道了! 您可以在 "oldCode=code;

上放置一个条件
    public string oldCode;
    public int counter = 0;
    string[] AffUrl = new string[100];
    string[] ImgUrl = new string[100];
    private void button1_Click(object sender, EventArgs e)
    {

        AffUrl[counter] = textBox1.Text;
        ImgUrl[counter] = textBox2.Text;

        string code = @"<div style=""float:left;padding-right:30px;padding-bottom:30px;"">
<div>
  <img src=""" + ImgUrl[counter] + @""" height=""200px"" width=""200px"" />
</div>
<div>
  <button onclick=""myFunction()"">Try it</button>
  <script type=""text/javascript"">
    function myFunction" + counter + @"() {
      var btn = document.createElement(""BUTTON"");
      window.open(""" + AffUrl[counter] + @""", ""_self"")
    }
  </script>
</div>
 </div>";
        if (counter == 0)
        {
            oldCode = code;
        }
        oldCode = string.Concat(code, oldCode);
        counter++;

        richTextBox1.Text = oldCode;
    }
}

}