动态DataList中的绑定图像 [英] Binding Image in Dynamic DataList
本文介绍了动态DataList中的绑定图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个存储不同类别的类,每个类别都有一个小图像和大图像。我将它存储在sharepoint列表中。我使用Dynamic Datalist来显示: -
我的类结构是: -
I have a class that stores different category and each category has a small image and big image. I am storing it in sharepoint list. I am using Dynamic Datalist to display this:-
My class structure is:-
public class Gallery
{
private string _category;
private string _smallImage;
private string _bigImage;
private string _id;
public string Category
{
get
{
return _category;
}
set
{
_category = value;
}
}
public string SmallImage
{
get
{
return _smallImage;
}
set
{
_smallImage = value;
}
}
public string BigImage
{
get
{
return _bigImage;
}
set
{
_bigImage = value;
}
}
public string ID
{
get
{
return _id;
}
set
{
_id = value;
}
}
}
我使用以下代码来显示它。
I am using following code to display it.
private void DisplayGallery()
{
try
{
List<Gallery> CategoryDetail = new List<Gallery>();
foreach (string category in DistinctCategory)
{
CategoryDetail = (from x in GalleryList where x.Category == category select x).ToList();
DataList dlCategory = null;
foreach (Gallery g in CategoryDetail)
{
dlCategory = ConstructDL(category, g.ID, g.SmallImage, g.BigImage);
dlCategory.DataSource = CategoryDetail;
dlCategory.DataBind();
}
placeHolder.Controls.Add(dlCategory);
dlCategory.RepeatDirection = RepeatDirection.Horizontal;
dlCategory.ID = "DTL";
}
}
catch (Exception ex)
{
Common.LogError(ex);
}
}
public DataList ConstructDL(string CategoryName, string ID, string SmallImage, string BigImage)
{
DataList dlCategory = new DataList();
MyTemplate headTemplate = new MyTemplate(ListItemType.Header);
headTemplate.category = CategoryName;
MyTemplate.ID = ID;
MyTemplate.smallImage = SmallImage;
dlCategory.HeaderTemplate = headTemplate;
MyTemplate.bigImage = BigImage;
dlCategory.ItemTemplate = new MyTemplate(ListItemType.Item);
return dlCategory;
}
public class MyTemplate : ITemplate
{
ListItemType _itemType;
public static string ID;
public static string smallImage;
public static string bigImage;
public string category;
public MyTemplate(ListItemType Type)
{
_itemType = Type;
}
public void InstantiateIn(System.Web.UI.Control container)
{
try
{
Literal lc = new Literal();
switch (_itemType)
{
case ListItemType.Header:
lc.Text = "<h1>" + category + "</h1>";
break;
case ListItemType.Item:
lc.DataBinding += new EventHandler(TemplateControl_Binding);
break;
}
container.Controls.Add(lc);
}
catch (Exception ex)
{
Common.LogError(ex);
}
}
private void TemplateControl_Binding(object sender, System.EventArgs args)
{
try
{
Literal lc;
lc = (Literal)sender;
DataListItem container = (DataListItem)lc.NamingContainer;
lc.Text += "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td class=\"tdImg\"> <img src='" + GetImageUrl(ID, smallImage) + "' alt='" + GetImageUrl(ID, bigImage) + "' önclick=\"ShowpopUp(this)\" /></td></tr></table>";
}
catch (Exception ex)
{
Common.LogError(ex);
}
}
public string GetImageUrl(object ID, object ImageName)
{
string imageUrl = string.Empty;
try
{
using (SPSite spSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
CLDance_Gallery uc = new CLDance_Gallery();
if (Common.isListExists(spWeb, uc.ListName))
{
SPList spList = spWeb.Lists[uc.ListName];
imageUrl = spWeb.Url + "/" + spList.RootFolder.Url + "/Attachments/" + ID + "/" + ImageName;
}
}
}
}
catch (Exception ex)
{
Common.LogError(ex);
}
return imageUrl;
}
}
我已经成功绑定了数据列表。但所有图像都是一样的。我只能看到所有图像控制的最后一个事件的图像。请帮帮我。
I have successfully binded the datalist. But all the images are same. I can see only last event's image only for all image control. Please help me.
推荐答案
这篇关于动态DataList中的绑定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文