1
我有一個字符串,一個接一個地設置縮略圖,沒有換行符。一旦圖像達到它們所在區域的最大寬度,圖像就會換行到下一行。我希望在每第四個縮略圖之後插入一個換行符,並且代碼幾乎可以工作,但它會在第一行之後添加一個新行圖像 - 這看起來不對。使用7個縮略圖圖像,應該只添加一個換行符,並且將在第4個圖像之後。在字符串中每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();
}
如何修改上面的代碼,使新線不再在第一個縮略圖之後添加?
我試過模數檢查,但我得到了同樣的確切問題:在第一個縮略圖之後添加一個新行,然後在第四個之後添加一個新行。我不知道爲什麼它必須在第一項後繼續換行,但這不是我想要發生的事情。 –
@SaraDeJaneiro 0%0 == 0.爲你的if語句設置一個排除。 – 48klocs
@Habib不應該是(i-1)%4 == 0 ??你的例子在第0行和第4行之後打印一個。我們想索引三。 –