在某些情况下在字符串中添加换行符

时间:2014-06-18 12:44:22

标签: c# html line-breaks sitefinity

我正在使用一个旧的Sitefinity网站,在某些产品页面上有一些可用的切割玻璃选项。这些列为缩略图,可以单击并打开到弹出窗口中。现在,代码被设置为创建包含缩略图部分和缩略图本身的div,并且它们作为连续字符串一个接一个地显示。当缩略图达到它们所在区域的宽度时,缩略图会下拉到下一行。现在的问题是需要将三个剪切类缩略图组合在一起,即使这意味着将它们移动到下一行。

我不知道该怎么办,因为我不想做更多必要的工作。此外,这不是我创建的代码 - 我必须编辑的其他人(此人无法回答问题)。

以下是设置包含缩略图的div的代码(GlassItem是包含每个切割玻璃缩略图图像的div的名称):

protected String BuildCutGlass(Guid CurrentPage)
    {
        StringBuilder Glass = new StringBuilder();

        using (MySqlConnection cn = new MySqlConnection(ConfigurationManager.ConnectionStrings["Sitefinity"].ToString()))
        {
            cn.Open();

            MySqlCommand cmd = new MySqlCommand("SELECT id, BrandID, GlassName, Thumbnail, LargeImage, Ordinal, (SELECT Window_Brands.BrandName FROM Window_Brands WHERE Window_Brands.BrandPage = BrandID) AS BrandName FROM Window_Brand_cutglass WHERE BrandID = ?PageID AND Thumbnail IS NOT NULL AND LargeImage IS NOT NULL ORDER BY Ordinal", cn);
            cmd.Parameters.Add(new MySqlParameter("PageID", CurrentPage.ToString()));

            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    if (reader["Thumbnail"].ToString().Length > 1 && reader["LargeImage"].ToString().Length > 1)
                        Glass.Append(String.Format("<div class='GlassItem'><a href='{0}' class='CutGlassPopup Icon'><img src='{1}' alt='{2}' width='77' height='77' /></a><a href='{0}' class='CutGlassPopup'>{2}</a></div>", Revere.GetImagePath(reader["LargeImage"].ToString()), Revere.GetImagePath(reader["Thumbnail"].ToString()), reader["GlassName"].ToString()));
                }
            }

            cn.Close();
        }

        return Glass.ToString();
    } 

1 个答案:

答案 0 :(得分:1)

这可能不是您正在寻找的快速解决方案,但是此代码难以使用的原因是因为您的数据检索代码与您的html生成代码混合在一起。我将为GlassItem创建一个类,并修改该方法以返回它们的列表。

public class GlassItem
{
    public string LargeImagePath {get;set;}
    public string ThumbnailPath {get;set;}
    public string GlassName {get;set;}
}

protected List<GlassItem> GetGlassItems(Guid CurrentPage)
{
    var items = new List<GlassItem>();

   using (MySqlConnection cn = new MySqlConnection(ConfigurationManager.ConnectionStrings["Sitefinity"].ToString()))
   {
       cn.Open();

       MySqlCommand cmd = new MySqlCommand("SELECT id, BrandID, GlassName, Thumbnail, LargeImage, Ordinal, (SELECT Window_Brands.BrandName FROM Window_Brands WHERE Window_Brands.BrandPage = BrandID) AS BrandName FROM Window_Brand_cutglass WHERE BrandID = ?PageID AND Thumbnail IS NOT NULL AND LargeImage IS NOT NULL ORDER BY Ordinal", cn);
       cmd.Parameters.Add(new MySqlParameter("PageID", CurrentPage.ToString()));

       using (MySqlDataReader reader = cmd.ExecuteReader())
       {
           while (reader.Read())
           {
               if (reader["Thumbnail"].ToString().Length > 1 && reader["LargeImage"].ToString().Length > 1)
                {
                    items.Add(new GlassItem 
                            { 
                                LargeImagePath = Revere.GetImagePath(reader["LargeImage"].ToString()), 
                                ThumbnailPath = Revere.GetImagePath(reader["Thumbnail"].ToString()), 
                                GlassName = reader["GlassName"].ToString()
                            });
                }   
           }
       }

       cn.Close();
   }

    return items;
}

然后你可以使用linq等来确定每行上你想要的项目,或者你也可以使用一个简单的循环。

    var items = GetGlassItems(new Guid("..."));
    StringBuilder Glass = new StringBuilder();

    for(int i = 0; i < items.Count();i++)
    {
        Glass.Append(String.Format("<div class='GlassItem'><a href='{0}' class='CutGlassPopup Icon'><img src='{1}' alt='{2}' width='77' height='77' /></a><a href='{0}' class='CutGlassPopup'>{2}</a></div>", 
                                    items[i].LargeImagePath, 
                                    items[i].Thumbnail, 
                                    items[i].GlassName));
        if(i % 5 == 0 )
        {   
            // new line every 5 items
            Glass.AppendLine();
        }
    }