Reputation: 1569
I have an old Sitefinity site that I am working on, and on certain products pages there are a selection of cut glass options available. These are listed as thumbnail images that can be clicked and opened into a popup window. Right now, the code is set up to create the divs containing the thumbnail section and the thumbnails themselves, and they are displayed one right after the other as a continuous string. The thumbnails drop down to the next line when they reach the width of the area they are in. The problem right now is that there are three cut class thumbnails that need to be grouped together, even if that means moving them to the next line.
I'm not sure how to proceed, as I don't want to have to do more work than necessary. Also, this is not code I created--it is someone else's that I must edit (the person is not available to answer questions).
Here is the bit of code that sets up the divs containing the thumbnails (GlassItem is the name of the div that encloses each cut glass thumbnail image):
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();
}
Upvotes: 0
Views: 106
Reputation: 13495
This probably isn't the quick fix you are looking for, but the reason that this code is hard to work with is because your data retrieving code is mixed in with your html generating code. I'd create a class for GlassItem
and modify the method to return a list of them.
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;
}
Then you can use linq etc to figure out which items you want on each line, or you can use a simple loop as well.
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();
}
}
Upvotes: 1