【发布时间】:2014-06-19 14:45:49
【问题描述】:
我有一个字符串,它一个接一个地设置缩略图,没有换行符。图像一旦达到它们所在区域的最大宽度,就会自动换行。我想在每 4 个缩略图之后插入一个换行符,我的代码几乎可以工作,但它会在第 1 个之后不断添加新行图片——这看起来不对。对于 7 个缩略图,应该只添加一个换行符,并且应该在第 4 个图像之后。
以下是用于调用并使用 SQL 数据库中的缩略图信息构建字符串的类信息:
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;
}
这是使用 GetGlassItems 类的代码。这是在每 4 项之后插入一个新行的地方,但也在第一项之后添加一个新行:
protected String BuildCutGlass(Guid CurrentPage)
{
var items = GetGlassItems(new Guid(CurrentPage.ToString()));
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].ThumbnailPath,
items[i].GlassName));
Console.Write("count: " + items.Count.ToString() + "<br />");
if (i == 4)
{
// new line every 4 items
Glass.Append("<div style='clear: both;'></div>");
}
}
return Glass.ToString();
}
如何编辑上述代码,以便在第一个缩略图之后不再添加新行?
【问题讨论】:
标签: c# sql count newline stringbuilder