我有一个清单,例如:
<ul id="list">
<li id="content_rptGalleryImages_galleryListItem_0" data-imageid="7" data-displayorder="0"></li>
<li id="content_rptGalleryImages_galleryListItem_1" data-imageid="5" data-displayorder="1"></li>
<li id="content_rptGalleryImages_galleryListItem_2" data-imageid="4" data-displayorder="2"></li>
</ul>
我希望能够访问每个列表项data-imageid
和data-displayorder
属性,并将它们添加到我后面代码中的字典中,例如:-
Dictionary<int, int> dict = new Dictionary<int, int>();
foreach(var li in list.Controls)
{
//find li
dict.Add(li.Attribute["data-imageid"], li.Attribute["data-displayorder"]);
}
我想要实现的目标可能吗?如果没有,你能建议一种替代方法吗?
根据要求编辑*中继器代码:
protected void rptGalleryImages_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
BrandImage image = (BrandImage)e.Item.DataItem;
HyperLink lnkGalleryImage = (HyperLink)e.Item.FindControl("lnkGalleryImage");
HyperLink lnkImageUrl = (HyperLink)e.Item.FindControl("lnkImageUrl");
LinkButton lnkBtnRemoveImg = (LinkButton)e.Item.FindControl("lnkBtnRemoveImg");
HtmlGenericControl galleryListItem = (HtmlGenericControl)e.Item.FindControl("galleryListItem");
lnkGalleryImage.Text = image.ImageLocation.Substring(image.ImageLocation.LastIndexOf("/")).Replace("/", "");
lnkGalleryImage.NavigateUrl = image.ImageLocation;
lnkImageUrl.Text = image.ImageUrl;
lnkImageUrl.NavigateUrl = image.ImageUrl;
lnkBtnRemoveImg.CommandArgument = image.ImageLocation;
galleryListItem.Attributes.Add("data-imageid", image.Id.ToString());
galleryListItem.Attributes.Add("data-displayorder", image.DisplayOrder.ToString());
}
}
谢谢