首页 > 解决方案 > 用于使用 StringBuilder 显示多个 HTML 页面而不是仅显示一个 (ASP.NET) 的语句

问题描述

我的 for 语句正在打印 10 个 HTML 页面,而我只需要一个带有 10 个替换和添加 div 标签的编辑过的 HTML 页面。我正在使用 Regex.Replace 查找链接并在之前添加一个 z-index div 标签,以便我可以用数字突出显示每个链接。

protected void Page_Load(object sender, EventArgs e)
    {
        string con = ("Data Source=Desktop-7k2gnco;Initial Catalog=trackingstats;"
       + "Integrated Security=SSPI");

        string queryString =

            "select CampaignId, ConvertedCreative from CampaignAdCopy where CampaignId = 24896;";



        using (SqlConnection connection =
                       new SqlConnection(con))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
               
                string CampaignId = reader["CampaignId"].ToString();
                string htmlCreate= reader["ConvertedCreative"].ToString();
                StringBuilder strContents = new StringBuilder();
                for (int i = 1; i < 11; i++)
                {
                   
                    string replacement = "<div style=\"z-indexer:1;background:#0c538e;width:2.25em;height:2.25em;text-align:center;color:white;\">" + i + "</div><a href=\"http://www.trackingstats.info/Process.aspx.c=/" + CampaignId + "&l=" + i + "\"";
                    Regex regex = new Regex("<a href=\"http://www.trackingstats.info/Process.aspx.c=24896&l="+ i +"\"", RegexOptions.IgnoreCase);
                    string result = regex.Replace(htmlCreate, replacement);
                    strContents.AppendLine(result);
                    
                 }
                Label1.Text = strContents.ToString();
            }
        }
    }

标签: c#asp.net

解决方案


推荐阅读